1
#include <stdio.h>

int main() {
    char *c = "test";

    while (*c != '\0') {
        printf("%c", *c);
        *c++; // or c++ both produced same result. ie test
    }

    return 0;
}

As *c++ should increase value and c++ should increase pointer address. But both are increasing pointer why?

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
user2681304
  • 129
  • 6

2 Answers2

2

This is due to operator precedence ++ has higher precedence than *

1

But both are increasing pointer why?

That's because of something called operator precedence. Take a look here

If you want to increase the value then you should be doing

(*c)++ //increment the value at address c

And as a bit of advice your code is more 'C' than 'C++'. Avoid raw pointers where possible and use the iostream library instead of 'printf'

pcodex
  • 1,812
  • 15
  • 16