C++ 打印数组元素内存地址C和C++,为什么输出不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6239833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:44:33  来源:igfitidea点击:

Printing array element memory adresses C and C++, why different output?

c++cmemorymemory-management

提问by W3ctor

Possible Duplicate:
How to simulate printf's %p format when using std::cout?

可能的重复:
如何在使用 std::cout 时模拟 printf 的 %p 格式?

I try to print out the array element memory addresses in C and C++.

我尝试打印出 C 和 C++ 中的数组元素内存地址。

In C:

在 C:

char array[10];
int i;
for(i =0; i<10;i++){
    printf(" %p \n", &array[i]);
}

I got the memory addresses: 0xbfbe3312, 0xbfbe3313, 0xbfbe3314, ....

我得到了内存地址:0xbfbe3312, 0xbfbe3313, 0xbfbe3314, ...

But if I try to make the same in C++:

但是如果我尝试在 C++ 中做同样的事情:

char array[10];
for(int i =0; i<10;i++){
    std::cout<<&array[i]<<std::endl;
}

I got this output:

我得到了这个输出:

?

P??
??
?

k?
?


?

??
??
?

克?
?


Why is it different? Should I use the coutdifferently in C++ to print out the memory addresses? How should I print out the memory addresses?

为什么不一样?我应该使用coutC++ 中的不同来打印出内存地址吗?我应该如何打印出内存地址?

回答by Xeo

Cast the address to void*before printing, in C++ the operator<<of ostreamis overloaded for (const) char*so that it thinks it's a c-style string:

void*在打印之前将地址强制转换为,在 C++ 中,operator<<ofostream被重载,(const) char*以便它认为它是一个 c 样式的字符串:

char array[10];
for(int i =0; i<10;i++){
    std::cout << static_cast<void*>(&array[i]) << std::endl;
}

Also see this answer of mine.

另请参阅我的这个答案

回答by Neil

The type of &array[i]is char*, and so cout<<thinks that you want to print a string.

的类型&array[i]char*,因此cout<<认为您要打印一个字符串。

回答by RC Howe

std::coutdoes not necessarily treat pointers as pointers. A char*pointer could be a string, for instance. Taking the address of an element in a chararray basically outputs the substring from that point.

std::cout不一定将指针视为指针。例如,char*指针可以是 a string。获取char数组中元素的地址基本上是从该点输出子字符串。

回答by ?ukasz Milewski

You must cast &array[i]to void*

你必须投射&array[i]void*

for(int i =0; i<10;i++){
    std::cout<<(void*)&array[i]<<std::endl;
}

This is because C++ streams work differently for different types. For example when you pass char*to it, your data is treated as a C-string - thus it is printed as a list of characters.

这是因为 C++ 流对不同类型的工作方式不同。例如,当你传递char*给它时,你的数据被视为一个 C 字符串 - 因此它被打印为一个字符列表。

You must explicitly tell C++ that you want to print an address by casting.

您必须明确告诉 C++ 您要通过强制转换来打印地址。

By the way (void*)is not the best way to do that as you should avoid C-like casting. Always use C++-style casting (static_cast, dynamic_cast, reinterpret_cast). In this case static_castwould do the job.

顺便说一句,(void*)这不是最好的方法,因为您应该避免类似 C 的强制转换。始终使用 C++ 风格的强制转换 ( static_cast, dynamic_cast, reinterpret_cast)。在这种情况下static_cast将完成这项工作。