Dec 18, 2012 at 1:21am UTC
Hello all, I would just like to find out why is it that when I try to assign the string "hello" to the variable myarray[1][0].mystring, the string "hello" gets assigned to both myarray[1][0].mystring and myarray[0][6].mystring?
myarray is from the struct array.
Thanks very much for any help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
const int ROWS=50;
const int COLS=6;
struct array
{
string mystring;
int myint;
int tracker;
};
int main(void )
{
struct array myarray[ROWS][COLS];
myarray[1][0].mystring="hello" ;
cout<<myarray[1][0].mystring<<"\n" ;
cout<<myarray[0][6].mystring<<"\n" ;
return 0;
}
Last edited on Dec 18, 2012 at 1:31am UTC
Dec 18, 2012 at 1:30am UTC
You set a value to myarray, but print out values from myarray2 . So the program should not have compiled.
Dec 18, 2012 at 1:32am UTC
Oops, I accidentally left it there when I was shortening the code for this forum. I've now removed it. The error still remains.
Dec 18, 2012 at 2:18am UTC
It still doesn't compile - so how are you saying it prints "hello". I don't see how you can claim it does.
Did you have any compiler errors / warnings. It might be instructive to read them
Dec 18, 2012 at 2:20am UTC
A 6 in the second index is out of bounds, which happens to overflow into the next row (namely into element [1][0]).