I don't understand why printing pp where pp is a double pointer to p returns adress of p while printing p returns a but p is a pointer to a. In general I don't get why printing pp doesnt return the adress of the pp pointer. the code looks the following:
int a[] = { 0, 1, 2, 3, 4 };
int *p[] = {a, a+1, a+2, a+3, a+4 };
int **pp = p;
main() {
printf(“...”, p, *p, **p);
printf(“...”, pp, *pp, **pp); }
//and when i print it looks like this:
p = address of p
*p = address of a
**p = 0
pp = address of p
*pp = address of a
**pp = 0
pis an array. It is not a pointer.int *p[] = {a, a+1, a+2, a+3, a+4 };declarespto be an array of pointers tointand initializes the elements ofpto the addresses ofa[0],a[1],a[2],a[3], anda[4].In
int **pp = p;, the arraypis automatically converted to a pointer to its first element. This is a rule in C: When an array is used in an expression and is not the operand ofsizeof, is not the operand of unary&, and is not a string literal used to initialize an array, it is automatically converted to a pointer to its first element. Sopis converted to&p[0]. So the declaration is equivalent toint **pp = &p[0];, which setsppto point to the first element ofp.In
printf(“...”, p, *p, **p);:pis automatically converted to a pointer to its first element, so&p[0]is sent toprintf. (Also note that the start of an array and the start of its first element are at the same place, so&pand&p[0]are the same address, albeit of different types.)*pis the first element ofp. (In detail,pis converted to a pointer to its first element,&p[0], so*pis*&p[0], which isp[0]. Then the value of that element is sent toprintf. (Technically, there is another conversion here;p[0]is an lvalue for the first element ofp, and it is automatically converted to its value.)For
**p, we know from above that*pisp[0], so**pis*p[0].p[0]points toa[0], so*p[0]isa[0].In
printf(“...”, pp, *pp, **pp); }:pppoints to the first element ofp, so the address ofp[0]is sent toprintf.In
*pp, sincepppoints to the first element ofp,*ppis that first element,p[0], andp[0]points toa[0], so the address ofa[0]is sent toprintf.In
**pp, we know from above*ppis&a[0], so*ppis*&a[0], which isa[0].