In am doing some experiment in C pointers and trying to understand its behaviour. The following are my assumptions for the below codes. Correct me if I am wrong. I have the following codes:
int n[5] = {3,6,9,12,15};
int *ptr = n;
ptr++;
printf("%d", *ptr); //Needless to say, the output will be 6
My assumption: The output above is 6 because ptr++ means ptr = ptr + 1 I am changing the value of ptr which is the address of n[0].
Now we take a look at the following scenario:
int n[5] = {3,6,9,12,15};
int *ptr = n;
*ptr++;
printf("%d", *ptr); //Why is the output still 6?
My question is: How do we interpret *ptr++? Does it means:
- *ptr = *ptr + 1 or
- *ptr = ptr + 1 or
- ptr = ptr + 1 or what?
By the way, when I print out values of n, it is still 3,6,9,12,15.
Sorry for 2nd question:
How shall we interpret *++ptr and ++*ptr then?