如何获取char数组中元素的地址? [英] How do I get the address of elements in a char array?
本文介绍了如何获取char数组中元素的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个char
数组,我需要获取每个元素的地址.
I have a char
array and I need to get the address of each element.
cout << &charArray
给我一个有效的地址,但是,如果我尝试获取特定元素的地址,则会吐出垃圾:
gives me a valid address, However if I try to get the address of a specific element, it spits out garbage:
cout << &charArray[0]
推荐答案
std::cout << (void*) &charArray[0];
对于char*
,有一个operator<<
的重载,它试图打印以nul结尾的字符串,该字符串认为您的指针指向其第一个字符.但是并不是所有的char数组都是以nul结尾的字符串,因此是垃圾.
There's an overload of operator<<
for char*
, that tries to print the nul-terminated string that it thinks your pointer points to the first character of. But not all char arrays are nul-terminated strings, hence the garbage.
这篇关于如何获取char数组中元素的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文