What is wrong with this variable

When I execute this I get a runtime error that says the variable narray is uninitialized, but as far as I can tell it is initialized! WHAT IS GOING ON!?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	const int ARRAY_LEN = 10;
	int narray[ARRAY_LEN] = {1,2,3,4,5,6,7,8,9,10};

	for (int index = ARRAY_LEN - 1; index >= 0; --index)
		cout << narray[ARRAY_LEN] << endl;

	return 0;
}
narray[ARRAY_LEN] is trying to access memory beyond the end of the array.

Remember that array elements are indexed from 0, so valid indices are 0 to (ARRAY_LEN - 1).

Did you actually mean to output the value of narray[index]?

Did you actually mean to output the value of narray[index]?


Lol I'm tired :P
The problem is line 11 your outputting narray[ARRAY_LEN] or in other words narray[10] which is not initialized. I think you meant
 
cout << narray[index] << endl; 
Lol I'm tired :P

No worries - it's easily done! Hope that solved the problem :)
Topic archived. No new replies allowed.