Why the pointer increases by 4 when the increment operator is used (ptr++)? Why there's something to do with int? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 */

4th Dec 2018, 3:53 PM
Konybud
Konybud - avatar
2 Answers
+ 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.
4th Dec 2018, 4:05 PM
Микола Федосєєв
Микола Федосєєв - avatar