for (int j=0; j<10; j++)
{
x = *p;
p++;
*p = x + j;
}
for (int k = 0; k < 10; k++)
{
cout << p[k] << " "; //edited
}
cout << endl;
return 0;
}
-------------------------------------------------
Based on theory,
The output should be
4 4 5 7 10 14 19 25 32 40
But now the output is
49 57943 4128964 4134664 0 0 0 0 0 0
That can't possibly be the output, as you print the same value ten times.
Aside from that, you have an out-of-bounds access in the last iteration of the first loop and after it, p points past the array, so p[1] is two elements past the array, resulting in undefined behavior.
Yeah, but the problems remain. You have one out-of-bound access in the first loop and since you're modifying p, all 10 accesses in the second loop are out-of-bounds.
It won't, because you changed what p points to in the first loop (p++;).
You incremented p ten times, so it now points to a location beyond the last element.