Why output is 10 rather not 9.
Code:
#include
using namespace std;
void main()
{
const size_t array_size=10;
int ia[array_size];
for (size_t ix=0;ix!=array_size;++ix)
ia[ix]=ix;
cout<
}
/*
my logic is:when + + X is equal to 10, you no longer perform the body of the loop statement
right result cout<
*/
thanks.

VulpesRank #55 · 29.2K reputationPosted Aug 6, 2012, 7:35 AM EDT
It then produced a garbage result i.e. whatever is in the next memory slot after ia[9].
That's because ix will have been incremented to 10 just before the expression :
ix!=array_size
fails and the for loop exits.
Hemant SrivastavaRank #581 · 4.2K reputationPosted Aug 6, 2012, 6:46 AM EDT
Ken HRank #2,315 · 610 reputationPosted Aug 6, 2012, 6:30 PM EDT
#include
using namespace std;
void main()
{
const size_t array_size=10;
int ia[array_size];
size_t ix; /* now declared at function scope */
for (ix=0;ix!=array_size;++ix)
ia[ix]=ix;
cout<
/*
i think "cout<
*/
Hemant SrivastavaRank #581 · 4.2K reputationPosted Aug 6, 2012, 8:19 PM EDT
Now the line cout<
So accessing out of scope element "ia[10]" will give any random output. It may be 10 or something else..
Ken HRank #2,315 · 610 reputationPosted Aug 6, 2012, 11:57 PM EDT
i think this compile system question.
thanks.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts