where is my mistake following this code..
next to every line i commented on what i think it does
where is my mistake in understanding of changing the index of the pointer
??
Code:
#include <stdio.h>
int main() {
int *ptr;
int arrayInts[10] = {1,2,3,4,5,6,7,8,9,10};
ptr = arrayInts; //ptr points at cell [0]=1
printf("The pointer is pointing to the first ");
printf("array element, which is %d.\n", *ptr); //print 1
printf("Let's increment it.....\n");
ptr++; //ptr points at cell [1]=2
printf("Now it should point to the next element,");
printf(" which is %d.\n", *ptr); //print 2
printf("But suppose we point to the 3rd and 4th: %d %d.\n", // print cells [2] [3] 3 4
*(ptr+1),*(ptr+2)); //ptr stays the same
ptr+=2; //ptr points at cell [3]=4
printf("Now skip the next 4 to point to the 8th: %d.\n",
*(ptr+=4)); //ptr points to cell[7]=8
//and we print the old ptr value= 4
ptr--; //ptr points to cell[6]=7
printf("Did I miss out my lucky number %d?!\n", *(ptr++)); //ptr points to cell[7]=8
//and prints the old ptr value 7
printf("Back to the 8th it is then..... %d.\n", *ptr); //ptr points to cell[7]=8
//and prints it
return 0;
}
my output :
1
2
3 4
4
7
8
i am supposed to get the output:
The pointer is pointing to the first array element, which is 1.
Let's increment it.....
Now it should point to the next element, which is 2.
But suppose we point to the 3rd and 4th: 3 4.
Now skip the next 4 to point to the 8th: 8.
Did I miss out my lucky number 7?!
Back to the 8th it is then..... 8.
the differnce comes from this line
ptr+=4
you said that first we print the old value then we add 4
looks like they first added 4 then printed the value
??