LastSecond959
LastSecond959

Reputation: 169

Post-Increment as Index Confusion

I would rather say this to the point by showing the code.

Feel free to make this post as duplicate if it already has the answer to it (because I haven't found one)

This is my code:

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    int test=3;
    int *arr=(int*)malloc (5*sizeof (int));
    for (int x=0; x<5; x++) *(arr+x)=x+1;
    *(arr+(test++))=999;
    for (int x=0; x<5; x++) printf ("%d ", *(arr+x));

    return 0;
}

Output:

1 2 3 999 5

The question is, there I say test++ which means test will be incremented to 4 right? But why 999 is inserted to the index [3] instead of [4]?

From what I know, test should be incremented before 999 assigned to arr. Is there any explanation for this piece of code? (Is it different? Because it results in the same thing when I thought it should be different)

*(arr+(test++))=999;
*(arr+test)=999; test++;

/*
Or,
arr[test++]=999;        //999 goes to [3] instead of [4]
arr[test]=999; test++;  //The right syntax should be like this right?
*/

Btw, pre-increment works just fine like if I do arr[++test]=999, 999 will be assigned to index [4].

I'm sorry if the way I speak is too messy, ask anything if you think something is missing from my explanation and I hope with this post, other people who also got confused can understand it.

Upvotes: 1

Views: 989

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311146

I can not reproduce the result

1 2 3 4 999 5

Running your program I got

1 2 3 999 5

I think there is a typo in your question.

As for the question

The question is, there I say test++ which means test will be incremented to 4 right? But why 999 is inserted to the index [3] instead of [4]?

then according to the C Standard (6.5.2.4 Postfix increment and decrement operators)

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).

So you may consider this statement

*(arr+(test++))=999

like

*(arr+(test))=999
++test;

Upvotes: 1

Abhishek Bhagate
Abhishek Bhagate

Reputation: 5786

Pre-increment operator : A pre-increment operator( ++i ) is used to increment the value of a variable before using it in a expression. In the Pre-Increment, value is first incremented and then used inside the expression.

Post-increment operator: A post-increment operator ( i++ ) is used to increment the value of variable after executing expression completely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented.

So, in your case, the test++ will first return the value, that is 3. Then the it will increment. Thus, the arr[test++] goes to index 3 instead of 4. If you would have done like this - arr[++test] = 999, then first the value of test would have been increased to the value 4 and then that would have been used as the index in the statement. That's the main difference between post-increment and pre-increment.

Hope this clears your doubt !

Upvotes: 0

0___________
0___________

Reputation: 68059

what is confusing you. The result is: 1 2 3 999 5 using gcc.

https://godbolt.org/z/fgm5Xg

Upvotes: 0

Related Questions