c - Can anyone please explain difference between ptr+1 and ptr[0]+1 -
assuming sizeof
integer 4 bytes , sizeof(int *) 8 bytes , i'm not getting why ptr +1 moves forward size of 8 bytes , ptr[0]+1 moves forward size of 4 bytes.
int main() { int a[] = {1, 2, 3}; int *ptr[3]; //array of 3 elements pointed integer int *b; ptr[0] = a; printf("a: %lu\n", a); printf("a + 1: %lu\n\n", a+1); printf("ptr: %lu\n", ptr); printf("ptr + 1: %lu\n", ptr+1); printf("ptr[0]: %lu\n", ptr[0]);//ptr[0] holds base address of array printf("ptr[1]: %lu\n\n", ptr[0] + 1 ); printf("&ptr: %lu\n", &ptr); printf("&ptr + 1: %lu\n", &ptr+1); }
if understand question correctly, have array of pointers int
(int *
). expect address of second element 4 bytes higher address of first element, , asking why not so?
if indeed question, answer size of pointer not same size of int
.
on 64-bit compiler, size of pointer 8 bytes, while size of int 4 bytes.
you can print out sizeof(int)
, sizeof(int *)
see clearly.
Comments
Post a Comment