0

In a C++ program I passed a variable by reference using pointer *n .I tried to increment pointer through statement "*n++" but it didn't work, later i used *n+=1 then it got incremented. Why it didn't run in the first case but ran correctly in 2nd case because i have studied that both means the same.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • 3
    `*n++` means `*(n++)`, not `(*n)++`. It *does* increment the pointer `n` though - which `*n += 1` doesn't - so it's not clear what the problem is. (Although my guess is that it's a terminology issue and you don't actually want to increment the pointer itself.) – molbdnilo Oct 26 '21 at 09:47
  • A code sample would help with the answer. – Tharsalys Oct 26 '21 at 09:48
  • Long story short, [operator precende](https://en.cppreference.com/w/cpp/language/operator_precedence) says that you first execute incement `++` and then dereference `*`. So you incremented a pointer and the dereferenced the original pointer (which did nothing). You need parantheses to change the precedence. – Yksisarvinen Oct 26 '21 at 09:53

0 Answers0