【发布时间】:2020-06-17 08:06:59
【问题描述】:
所以,我最近一直在研究字符数组,我正在尝试打印字符数组中每个元素的地址。
char a[4] = {'i','c','e','\0'};
for(int i = 0; i < 3; ++i){
cout<<(void*)a[i]<<" ";
cout<<&a[i]<<" ";
cout<<a[i]<<endl;
}
上面的代码给了我以下输出:
0x69 ice i
0x63 ce c
0x65 e e
test.cpp: In function ‘int main()’:
test.cpp:29:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
cout<<(void*)a[i]<<" ";
^
我对@987654323@ 的输出感到不舒服。字符地址不应该相隔 1 个字节。我看到0x69,然后是0x63,然后是0x65。有没有原因。地址表示和它所显示的警告标志之间是否存在关系。
【问题讨论】: