user6219654
user6219654

Reputation: 91

Complex Pointer Statements in C

I was given this code:

#include <stdio.h>

int main(void)
{
    char *p[2][3] = {{"zyx","wvu","ts"},{"rqponm","lkjihgfe","dcba"}};

    printf("%c \n",***p);
    printf("%c \n",(*(*(p+1)+1))[6]);
    printf("%c \n",**(p[1]+2));
    printf("%c \n",*(*(p+1)+1)[6]);
    printf("%c \n",**p[1]);
    printf("%c \n",*(p[1][2]+2));
    return 0;
}

I ran that code but I can't figure out why each line prints what it prints. In addition, I know that the fourth printf doesn't print anything and makes an error; it should be like that.

Upvotes: 0

Views: 293

Answers (1)

Shubham
Shubham

Reputation: 247

char *p[2][3] = {{"zyx","wvu","ts"},{"rqponm","lkjihgfe","dcba"}}

will print z,f,d, garbage, r, b

Here at the bottom level we have characters ‘z’,‘y’,’x’,’w’,……. Now from the definition char *p[][] , we have made a 2D array of character pointers. i.e. the 2D array will contain addresses only.

How 2D array is implemented: It is the 1D array containing the address of other 1D arrays. So here we have p[2][3] i.e a 1D array with two elements will be created, and each of those two elements will contain the address (base address) of another 1D array with 3 elements. And since we are creating this 2D array as array of character pointers, therefore those 3 elements will contain the addresses of other 1D arrays (of undefined size) which are holding our character values. Here is the image of all the arrays created and values stored in these arrays: Image showing all the arrays with names

To be printed:

1). ***p or *(*(*p)) :

  • Here p contains the base address of the 1D array (P) of two elements. *p will extract the value stored at that base address, which is ‘address of G’.

  • *(*p)” will be the value stored at the location pointed by the ‘address of G’ which is base address of array G i.e. ‘Address of A’

  • *(*(*p)) will be the value stored at the location pointed by the ‘address of A’ which is the character ‘z’.
  • Therefore ***p will return ‘z’.

2). (*(*(p+1)+1))[6] :

  • As p contains the based address of the 1D array i.e. the address of first element of 1D array P.
  • p+1 will be the next address to p, which is the address of second element of 1D array P.
  • *(p+1) will be the value stored at ‘p+1’, which is ‘address of H’ which contains the base address of H array (address if 1st element of H).
  • *(p+1)+1 will be the next address which will be the address of 2nd element of H.
  • *(*(p+1)+1) will be the value stored at the 2nd element of H which is the address of E (base address of E array.
  • *(*(p+1)+1)[6] will be the 7th term in that array which is ‘f’.

3). **(p[1]+2):

  • Here p[1] points to the second value of the array pointed by p, which is ‘base address of H’ (which points to the first element in H array).
  • p[1]+2 will be the second next value to the ‘base address of H’ (or address which points to the first element in H array) which is ‘address to the third element in H array’.
  • *(p[1] + 2) will be value point by the ‘address to the third element in H array’ which will be the ‘value of the third element of H’ which is ‘base address of F’.
  • **(p[1] + 2) will be the value stored at the ‘base address of F’ which is ‘d’.

4). *(*(p+1)+1)[6]) :

  • As calculated in the case-2 above, ‘*(p+1)+1’ contains the ‘address of 2nd element of H’. Now here ‘*(p+1)+1)[6]’ means 7th value next to it which would point to some garbage address. And ‘((p+1)+1)[6])’ will be the value stored in that garbage address which could be any garbage value.
  • Observe the difference here: It is not asking about the 7th value next to the value at 2nd element of H, (if it is, the answer would be ‘f’) but instead, it is asking about the 7th value next to the address of 2nd element of H.

5). **p[1] :

  • Here p[1] will be the value at the second element of array pointed by ‘p’ which is ‘base address of H’.
  • *p[1] will be the value pointed by the ‘base address of H’ which is ‘base address of D’.
  • **p[1] will be the value pointed by the ‘base address of D’ which is ‘r’.

6). *(p[1][2]+2):

  • Here p[1][2] will be the value at the 2nd row’s (H-array) 3rd element which is ‘base address of F, which further is the address of ‘d’ character).

  • p[1][2]+2’ will be the second next value to the ‘address of d’ which is ‘address of b’

  • *( p[1][2]+2) will be the value at ‘address of b’ which is ‘b’.

Upvotes: 1

Related Questions