user33061
user33061

Reputation: 1875

2d array error c++

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?

Upvotes: 0

Views: 700

Answers (2)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506847

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?

Upvotes: 6

Jimmy
Jimmy

Reputation: 91432

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.

Upvotes: 5

Related Questions