views:

330

answers:

2

I'm trying to run a c++ 2d array (pretty simple file) and it works but an error (at least I think it's an error) appears on the end.

The code for the array is;

int myArray[10][10];
for (int i = 0; i <= 9; ++i){

 for (int t = 0; t <=9; ++t){

  myArray[i][t] = i+t; //This will give each element a value

 }

}

for (int i = 0; i <= 9; ++i){

 for (int t = 0; t <=9; ++t){

  cout << myArray[i][t] << "\n";

 }

this prints the array properly but adds

"0x22fbb0"

on the end. What is this and why does it happen?

+5  A: 

The error is not in the code you posted. do you have another cout afterwards?

the 0x22.... looks like a memory address, so specifically you might have a line that reads

cout << myArray;

somewhere.

Jimmy
Thanks, I'd just put cout << myArray in later again outside the loop by mistake
+6  A: 

The code you showed is fine so far. The address printed does not seem to be printed from that part of your code. I can imagine two situations for that.

  • You accidentally print myArray[i] or myArray and forgot to apply the other index. As an array value converts to the address of its first element, it causes an address being printed.
  • You accidentally print cout itself like cout << cout. cout has an implicit conversion to a pointer type (it is used to check for sane state like in if(cout) { ... }) and this will cause an address being printed too.

It could be a totally other situation. Can you paste the code that appears after the two loops?

Johannes Schaub - litb
that's interresting about the cout << cout :) never thought about that
Jimmy