+ 1
Why the pointer increases by 4 when the increment operator is used (ptr++)? Why there's something to do with int?
int a[5] = {22, 33, 44, 55, 66}; int *ptr = NULL; ptr = a; /* point to the first array element */ printf("%d %x\n", *ptr, ptr); /* 22 */ ptr++; printf("%d %x\n", *ptr, ptr); /* 33 */ ptr += 3; printf("%d %x\n", *ptr, ptr); /* 66 */ ptr--; printf("%d %x\n", *ptr, ptr); /* 55 */ ptr -= 2; printf("%d %x\n", *ptr, ptr); /* 33 */
2 Respostas
+ 2
That's because sizeof(int) = 4. So pointer watches the next int. Pointer to double increments by 8, in order to point to the next double.