int* testArray[10];
for (int i = 1; i <= 10; i++)
{
testArray[i] = new int();
*testArray[i] = i; //Why does this work if the below doesn't?
}
*testArray[0] = 5; //Produces an error
cout << *testArray[0]; //Prodcues an error
Unhandled exception at 0x0117109f in MemoryTester.exe: 0xC0000005: Access violation reading location 0xcccccccc.
How come?
Last edited by Bullet Pulse on Mon May 24, 2010 6:57 pm, edited 1 time in total.
2 things. First: it should be i < 10 not i <= 10. Array indexing of an array of n values is accessed with [0] to [n-1]. Secondly, i should start at 1, because otherwise [0] does not get allocated.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
What your doing wrong is simple. You have your array as 10 elements, but your putting new ints at [1] instead of [0]. So the for loop works perfectly, but when you try to access [0] it throws an error.
Live-Dimension wrote:Don't you mean i should start at 0?
What your doing wrong is simple. You have your array as 10 elements, but your putting new ints at [1] instead of [0]. So the for loop works perfectly, but when you try to access [0] it throws an error.