Reputation: 55
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k,*ptr=NULL;
int arr[]={1,2,3,4,5,6,7,8,9,10};
ptr=arr;
printf("%d ",*ptr++);
printf("%d ",*(ptr++));
printf("%d ",(*ptr)++);
printf("%d ",*++ptr);
printf("%d ",++*ptr);
}
Why does the second printf
print the number 2
? It should print 3
.
Upvotes: 0
Views: 791
Reputation: 754280
As everyone else said, the distinction is between pre-incrementing (where the increment occurs before the value is fetched) and post-incrementing (where the value is fetched and then the increment occurs). The value 2 should be printed, of course.
Maybe this assertion-laden code will help. The assert()
macro stops the program if the condition specified is false when it is executed. The assertions do not fire.
The assertions show how the value of ptr
changes, and also how the values in the array change.
#include <assert.h>
#include <stdio.h>
int main(void)
{
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int *ptr = arr;
assert(ptr == &arr[0]);
printf("%d\n",*ptr++); // print 1; ptr = &arr[1]
assert(ptr == &arr[1]);
printf("%d\n",*(ptr++)); // print 2; ptr = &arr[2]
assert(ptr == &arr[2]);
assert(*ptr == 3);
printf("%d\n",(*ptr)++); // print 3; ptr = &arr[2]; arr[2] = 4
assert(ptr == &arr[2]);
assert(*ptr == 4);
printf("%d\n",*++ptr); // print 4; ptr = &arr[3]
assert(ptr == &arr[3]);
assert(*ptr == 4);
printf("%d\n",++*ptr); // print 5; ptr = &arr[3]; arr[3] = 5
assert(ptr == &arr[3]);
assert(*ptr == 5);
printf("Offset: %d\n", (int)(ptr - arr));
for (int i = 0; i < 9; i++)
printf("a[%d] = %d\n", i, arr[i]);
return 0;
}
Output:
1
2
3
4
5
Offset: 3
a[0] = 1
a[1] = 2
a[2] = 4
a[3] = 5
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
Upvotes: 1
Reputation: 3984
The *ptr++
returns the value at ptr
and then increments it,so in the second printf()
statement it only returns 2 and then increments it to 3.
Upvotes: 0
Reputation: 5525
It should print 2, because the postfix operator ++ first returns the value and then increments it. The fact that you added brackets around it (*(ptr++)) does not influence the increment. The value will be incremented after the whole line.
Looking at disassembly might help you to see what happens at that line.
Upvotes: 0
Reputation: 122423
post increment operator increments the variabl after accessing the value.
So, after getting *ptr
, which is 2, ptr
increases itself.
Upvotes: 1
Reputation: 8747
the * operator applies to the result of p++, which is the value of original p (prior to the increment). So it prints 2
. I you want 3
to be printed you should do (++p) which returns the incremented value.
Upvotes: 0
Reputation: 9753
*ptr++ first dereferences the pointer which gives 2, then increments the pointer The expression *(ptr++) is a post increment expression so the value of of that expression is ptr and then it is incremented. So the result of the expression is that it still points to the 2
Upvotes: 0
Reputation: 455
Not, because ptr++ return the value BEFORE the incrementation, so the value is 2.
Upvotes: 0