user2877144 user2877144 - 3 years ago 109
C Question

C double pointer's array notation equivalence

If I have the following double pointer and I use it in an array form, which of the mentioned pointer forms is it equivalent to?

char** argv = malloc(2*sizeof(char*));

//argv[0] == (argv + 0), *(argv + 0), or (*argv + 0)?

//Same way, what would argv[0][0] be equivalent to in terms of *(*(argv +i)+j), etc.?

Answer Source

//argv[0] == (argv + 0), *(argv + 0), or (*argv + 0)?

Well here both *(argv + 0) and (*argv + 0) are both equivalent to argv[0]. This can be easily know by printing the output. You can do something like this:

char** argv = malloc(2*sizeof(char*));

for(int i = 0; i < 2; i++){
    argv[i] = malloc(1);

//printing the values:
printf("%p == %p\n", argv[0], argv + 0);
printf("%p == %p\n", argv[0], *(argv + 0));
printf("%p == %p\n", argv[0], *argv + 0);


0x8e84018 == 0x8e84008 //not equal
0x8e84018 == 0x8e84018 //same
0x8e84018 == 0x8e84018 //same

see here for complete example:

it's not true in the case of (argv + 0) because (argv + 0) points to the address of the char** argv itself whereas argv[0] points to the address of the first element (which of the type char*)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download