0

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?

3
  • This runs just fine for me, and the code looks solid. Which compiler and system are you using? Commented Dec 11, 2008 at 17:38
  • Can you create a small complete program that has this error and copy and paste it into your question? Your program is incomplete and there is nothing wrong with the logic, perhaps you are outputting something else afterwards? Commented Dec 11, 2008 at 17:39
  • this question is virtually identical to your other question (the other one actually had the bug in it). Please close this one. Commented Dec 11, 2008 at 21:06

2 Answers 2

6

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?

Sign up to request clarification or add additional context in comments.

1 Comment

that's interresting about the cout << cout :) never thought about that
5

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.

1 Comment

Thanks, I'd just put cout << myArray in later again outside the loop by mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.