Having a problem with using arrays

Hello I am a beginner in c++, can anyone explain to me when I run my code, oldx[0]=200??? here is my code below.. Thanks in advance!

#include "stdafx.h"
#include "math.h"
#include "iostream"

double oldx[3], oldy[3];

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{oldx[0]=100,oldx[1]=200,oldx[2]=200,oldx[3]=100;
oldy[0]=100,oldy[1]=100,oldy[2]=200,oldy[3]=200;

for (int i=0;i<4;i++)
{cout<<"oldx["<<i<<"] = "<<oldx[i]<<endl;

}


return 0;
}
Last edited on
It doesn't.
you mean it doesn't come out as oldx[0]=200 for you when you run it???
omg... Ive tried creating new projects and its all the same...
Here is is tidied up and build with a modern compiler:

http://ideone.com/e5kwN
Thanks for the help! If I may ask, what compiler are you using?? I'm using Microsoft Visual Studio 2010
It's at the top of that page: language: C++ (gcc-4.3.4)
Last edited on
There is a problem with your code. When you create an array of 3 elements (double oldx[3]), the valid indices are 0, 1 and 2. Accessing oldx[3] is invalid and is writing to/reading from memory that is not part of the array.

For more info, see this tutorial page.

http://cplusplus.com/doc/tutorial/arrays/
If I had to guess, oldy[3] has the same address as oldx[0]. You can test it yourself by fetching the addresses of these locations.
Topic archived. No new replies allowed.