Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Confusion over pointer index operator

I am a little bit confused about pointer index operator in C. I will try to explain my question with an example:

int array[5] = {1,2,3,4,5};
int *p;
p = array;
p[2]++;

In the fourth line, I know that it increments the second index of array. However, when I see an index operator, I convert it.

For instance, I converted p[2]++ to *(p+2)++. According to the operator precedence table, in the statement of *(p+2)++, the increment and dereferencing operators have the same precedence, but increment takes precedence due to right associativity. Therefore, it becomes *(p+3). Then, this statement cannot change any value and just points third index of array.

Why does p[2]++ increment the second index of the array? What is wrong in my perspective?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

p[2]++ is equivalent to (*(p+2))++, not *(p+2)++. You need an extra set of parentheses to maintain the precedence from the original expression.

Without them you've got *(p+2)++ which, as you've noted, is equivalent to *((p+2)++). This has a different meaning from the original expression since it splits up the +2 and the *. They need to be done in the same step since [2] is a single atomic operation.

over 4 years ago · Santiago Trujillo Report

0

As already commented p[2]++ can be converted to (*(p+2))++, because p[2] is the element you want to increment. Think that when incrementing indexes is usually done like p[i++]

over 4 years ago · Santiago Trujillo Report

0

In math, if a=b+c,then a*d is not b+c*d but (b+c)*d. Likely, p[2] is not be taken by *(p+2) but (*(p+2)) to avoid any change on precedence.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!