0

I was revisiting pointers when I had this doubt.

int *ptr;
int arr[5] = {10,20,30,40,50};
ptr = &arr[0];

Now printf("Value: %d",*ptr); would print 10

if I do:

ptr++;
printf("Value: %d",*ptr);

the result would be: 20

Similarly if I do:

*ptr++;
printf("Value: %d",*ptr);

the result is: 30

But

printf("Value: %d",++*ptr);

gives me 31

I thought since ptr is of type int when I increment it, it would jump 4 bytes to the next memory location. But why does it show the same behavior for *ptr++ and ptr++ and not for ++*ptr?

noMAD
  • 7,744
  • 19
  • 56
  • 94
  • possible duplicate of [why does *ptr++ act like *(ptr++) and not (*ptr)++?](http://stackoverflow.com/questions/6271028/why-does-ptr-act-like-ptr-and-not-ptr) – Oliver Charlesworth Apr 08 '12 at 20:46

5 Answers5

8

Because of precedence.

  • *ptr++ is the same as *(ptr++), the pointer is incremented, but its previous value is dereferenced.
  • ptr++ is the same as, well, ptr++.
  • ++*ptr is the same as ++(*ptr), i.e. it increments the thing being pointed to, not the pointer.
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • When i do `*(ptr++)` shouldn't it dereference the value of `ptr` and increment it rather than incrementing the pointer to the next memory location? Isn't that what happens with `++(*ptr)`? – noMAD Apr 08 '12 at 20:47
  • 1
    @noMAD: No, because in `*(ptr++)`, the parentheses mean that `++` is applied to `ptr`, not to `*ptr`. – Oliver Charlesworth Apr 08 '12 at 20:48
3

When you do * ptr++, you are actually doing * (ptr++), so the pointer get incremented, not the variable it points to

When ++ *ptr, it's actually ++(* ptr), so it increments the value returned by the pointer

jpalecek
  • 47,058
  • 7
  • 102
  • 144
DiamRem
  • 604
  • 1
  • 4
  • 9
2

The first snippet is obvious: it prints what ptr points to, i.e. 10.

The second one, moves the pointer forward of one element, which then points to the next element, i.e. 20.

The third snippet is exactly the same as the previous one, because its first instruction increments the pointer and returns the unincremented value, which is dereferenced, but its result is discarded; what is dereferenced in the printf is the incremented pointer, which now points to 30.

The last snippet is different: ++*ptr is ++(*ptr); *ptr dereferences ptr (which already points to 30), yielding 30, and ++ increments such value, which becomes 31.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
1

The order of C Precedence from high priority:

()
++
* & for pointers
* /
+ -

so,

 *ptr++ equivlant to *(ptr++)
 ++*ptr equivlant to ++(*ptr)

and for this line *ptr++; it will only increment the ptr pointer.

Mina Samir
  • 162
  • 12
1

The trick to this is that whenever both are unary operators, then associativity is from right to left.

*ptr++ --> first increment pointer to next location and then dereference the value at that location.

Similarly *++ptr -->first increment pointer and then dereference.

++*ptr --> first dereference value at ptr and then increment the value.