2

So I'm new to C and tested some things with pointers and I have a question about the following printf:

char txt[] = "thisIsAQuestion";
char *ptr = &txt[9];
printf("%c\n", ++(*ptr));
printf("%c\n", *ptr);

So following my "knowledge", it would go something like this:

The pointer points at the value 'e'. Then if I execute the first printf, first thing which is executed is the command which is in the (), so the dereference of the pointer *ptr, because it has higher priority over the Prefix Increment. Now what I think is that the ++ will act like (*ptr + 1), because the pointer got dereferenced already, and increments the value that the pointer is pointing to, but not change the pointer itself. So it would be 'f'.

But now when I run the second printf, it shows me that the pointer still points at 'f' and didn't "go back" to 'e'.

Is there an error in my thoughts? Or is there something else I didn't consider?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    Um... The pointer does not increment. It is `txt[9]` that increments and become `'f'`. There's no `'e'` in your array anymore. But you stated it yourself. Why are you talking about the pointer's being incremented? – AnT stands with Russia May 10 '19 at 15:23
  • `ptr` does not change it's value in this program, except when initialized. – Eugene Sh. May 10 '19 at 15:24
  • 1
    It is not about priorities but syntax. `(*ptr)` is what is being pointed to and `++(*ptr)` increments that. – Weather Vane May 10 '19 at 15:24
  • `++(*ptr)` - get the char value pointed to in memory by `ptr` and increment the value. `++ptr` - increment where `ptr` points to in memory – Chris White May 10 '19 at 15:25
  • Also, there's no such thing as "priority" between two prefix operators. The concept of "priority" is not applicable at all here. – AnT stands with Russia May 10 '19 at 15:25
  • 1
    This has nothing to do with pointers, really. You might as well ask why `int i = 5; printf("%d\n", ++i); printf("%d\n", i);` prints `6` two times instead of going "back" to 5. – melpomene May 10 '19 at 15:27

2 Answers2

0

You're missing the part, that the pointer points to an address an prefix ++ operator changes the value of the operand.

++ will act like (*ptr + 1)

No, rather it acts like *ptr = (*ptr + 1).

So, (*ptr) results in a value of e, (as expected), and then, by application of ++, the value is incremented and stored into the same memory location.

  • The result of the prefix increment operator is the new value, which gets passed on as the argument to printf() - it prints that value.

  • For the second printf() statement, you're already printing the incremented value.

Related, quoting C11, chapter 6.5.3.1

The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++E is equivalent to (E+=1).[...]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

The char *ptr = &txt[9]; points to the txt but start from e character.

The printf("%c\n", ++(*ptr)); statement at first increase e character and print it as f (e + 1 = f)

And the printf("%c\n", *ptr); statement just print a character where pointed to (and its f) because e value changed with ++(*ptr).