Search code examples
cpointerstypespointer-to-pointer

What exactly int/char when we declare double pointer(**q)


Lets take 64 bit machine

where pointer is of 8 bytes in 64 bit machine

int *p ; // it is a pointer to integer variable so when i increment p
         // i.e., p++ it will increment by 4 

char *r; // It is pointer to character . 
         // So if i increment 'r' it will increment by 1

int **q ; // if i increment q  ie.,q++ it will increment 8 bytes 

i tried this peace of code if any thing wrong please correct me

int a=10; 
int *p; 
char *r; 
int **q; 
p=&a; 
q=&p; 
printf("p= %p\t r= %p\t q=%p\n",p,r,q);
 printf("p(increment)= %p\t r (increment)= %p\tq (increment)= %p ",++p,++r,++q); 

output

 p= 0x7fff669bb1bc r= 0x7fff669bb2a0 q=0x7fff669bb1a0
 p(increment)= 0x7fff669bb1c0 r (increment)= 0x7fff669bb2a1 q (increment)= 0x7fff669bb1a8

what is role of int/char/float in double pointer?


Solution

  • To quote the property of the postfix ++ operator, from C11, chapter §6.5.2.4, Postfix increment and decrement operators, (emphasis mine)

    The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). [...]

    • In case of int *p ;, p is a pointer to type int, hence the increment will be based on sizeof(int)

    • In case of int **p ;, p is a pointer to type int *, hence the increment will be based on sizeof(int *)