I am trying to assign values within an array within the condition of a for loop:
#include <iostream>
using namespace std;
int* c;
int n;
int main()
{
scanf("%d", &n);
c = new int[n];
for (int i = 0; i < n; c[i] = i++ )
{
printf("%d \n", c[i]);
}
return 0;
}
However, I am not obtaining the desired output, for n = 5, 0 1 2 3 4. Instead, if I am using the instruction, c[i] = ++i, I am obtaining the output -842150451 1 2 3 4. Could you please explain me we does my code behave like this and how can I correct it?
The value of the expression
++iis the value afterihas been incremented. So if it started at 0, you assign value 1 the first time and so on. You can see where the value got assigned, but asking why it got assigned there opens a can of worms.Using
iin an expression whereiis modified viai++or++iis undefined behavior unless there is a so-called “sequence point” in between the two. In this case, there isn’t. See Undefined behavior and sequence points for this rather complicated part of the language.Although the behaviour is undefined by the standard, and may not be consistent from one run to another, clearly your program has done something. Apparently it didn’t assign to index 0 at all (at least, not before the first print, which is understandable considering that the loop body happens before the last part of the “for”), so you got whatever just so happened to be in that raw memory when it was allocated to you. It assigned
1to index1and so on.This means that it may also have attempted to assign the value
5toc[5], which is a class of bug known as a “buffer overrun”, and more undefined behavior on top of what you’ve already got. Attempting to assign to it probably overwrites other memory, which on any given day may or may not contain something important.The fix is to assign some value to
c[0], and don’t try to assign toc[5], which doesn’t exist anyway, and don’t try to invalidly usei“at the same time as” incrementing it. Normally you’d write this:If you’re desperate for some reason to assign in the third clause of a for loop, you could use the comma operator to introduce a sequence point:
But of course if you do that then you can’t print the value of
c[i]in the loop body. It hasn’t been assigned yet, because the third clause isn’t evaluated until the end of each loop.You could also try
c[i] = i+1, ++i, but not++i, c[i] = ibecause then we’re back to trying to assign toc[5], on the last iteration.