2

I am new to C. Sorry for such a basic question.

int ArrayIndex = 0;
int intArray[ArrayIndex++] = somevalue;

I have read a book that says when exactly postfix increment/decrement is performed is not a simple question to answer. The book also says vaguely about a concept called sequence points to answer the question. It says updating the value[incrementing/decrementing] will take place between the previous and next sequence point. The example for such a sequence point is end of an expression statement.

My question is, will the above code snippet always assign some value to array index zero and increment ArrayIndex, in all compilers/platforms? Is there a chance that ArrayIndex is incremented first and then somevalue is assigned to intArry[1];?

Can anybody shed some light?

2 Answers 2

2

My question is, will the above code snippet always assign some value to array index zero and increment ArrayIndex, in all compilers/platforms?

Answer: Yes

Is there a chance that ArrayIndex is incremented first and then somevalue is assigned to intArry[1];?

Answer: If you use

int intArray[++ArrayIndex] = somevalue;

this will increment ArrayIndex and then assign value to array index 1

Sign up to request clarification or add additional context in comments.

Comments

2

++ArrayIndex increments ArrayIndex and evaluates to the new value of ArrayIndex.

ArrayIndex++ evaluates to the old value of ArrayIndex, and increments ArrayIndex.

This should answer your question. (Hint: the answer is yes).

5 Comments

Naming conventions are quite irrelevant here to say the least.
@AlexeyFrunze You're right. As long as he's consistent this shouldn't bother.
It's not that. First, naming conventions weren't asked about. Second, they are subjective or specific to the team/project. Third, the question does not have low readability (like the poorly formatted long code you've seen in some other questions) due to the naming convention chosen. So, there's not a single objective reason to point at it at all.
@AlexeyFrunze Sometimes it doesn't hurt if you add additional notes/improvements even if they were not asked. But, as I said before, I agree with you in this case.. I shouldn't have mentioned it and that's why I edited my answer and removed it.
Sometimes yes, I agree, but not this time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.