简体   繁体   English

2D数组错误C ++

[英]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. 我正在尝试运行c ++ 2d数组(非常简单的文件),它可以工作,但是最后出现错误(至少我认为是错误)。

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" “ 0x22fbb0”

on the end. 最后。 What is this and why does it happen? 这是什么,为什么会发生?

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. 您不小心打印了myArray [i]或myArray,却忘记应用其他索引。 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本身打印为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. cout隐式转换为指针类型(用于检查健全状态,如if(cout) { ... } ),这也会导致打印地址。

It could be a totally other situation. 可能完全是另外一种情况。 Can you paste the code that appears after the two loops? 您可以粘贴两个循环之后出现的代码吗?

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 0x22 ....看起来像一个内存地址,因此特别是您可能会有一行读取

cout << myArray; cout << myArray;

somewhere. 某处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM