Saturday 15 September 2012

C Pointers : *ptr++ or *(ptr +1) -



C Pointers : *ptr++ or *(ptr +1) -

this question has reply here:

incrementing pointer (ptr++) , (*ptr++) 3 answers

is *ptr++ same *(ptr + 1)? have array named arr. > int *ptr = arr; array conatins simple arithmetic integers such as: arr[0] = 0, arr [1] = 1..

printf(" initial value of *ptr %d \n",*ptr); *ptr++; printf(" value of *ptr after *ptr++ %d \n",*ptr); *(ptr++); printf(" value of *ptr after *(ptr++) %d \n",*ptr); *(ptr+1); printf(" value of *ptr after *(ptr+1) %d \n",*ptr); *ptr+1; printf(" value of *ptr after *ptr+1 %d \n",*ptr);

the output is:

0 1 2 2 2

is not lastly value supposed 3 , 4 ? not *ptr++ = *ptr+1 or = *(ptr+1)?

please help. learning c concepts.

*(ptr++), *ptr++ both result in incrementing value of pointer not +1 expression.

let &a[0] i.e address of start of array = 0x1000 (for simplicity). allow assume size of each element in array 4 bytes.

x = *ptr++; //x holds 0x1004 : ptr holds 0x1004 x = *(ptr++); //x holds 0x1008 : ptr holds 0x1008 x = *(ptr+1); //x holds 0x100c : ptr still holds 0x1008 (we have not modified ptr) x = *ptr+1; //x holds 0x100c : ptr still holds 0x1008 (we have not modified ptr)

hope helps

c pointers

No comments:

Post a Comment