C++ 请问,;如果x是指针,那么x与x不同?
所以…我有以下代码:C++ 请问,;如果x是指针,那么x与x不同?,c++,pointers,ampersand,C++,Pointers,Ampersand,所以…我有以下代码: int main(void) { const char *s="hello, world"; cout<<&s[7]<<endl; return 0; } int main(无效) { const char*s=“你好,世界”; couts[7]是字符'w',所以&s[7]成为字符'w'的地址。当您将char*类型的地址传递给cout时,它将打印从字符开始到字符串结尾的空字符的所有字符,也就是说,它将继续从'w'开始打印字符,直到找到\0
int main(void)
{
const char *s="hello, world";
cout<<&s[7]<<endl;
return 0;
}
int main(无效)
{
const char*s=“你好,世界”;
couts[7]
是字符'w'
,所以&s[7]
成为字符'w'
的地址。当您将char*
类型的地址传递给cout
时,它将打印从字符开始到字符串结尾的空字符的所有字符,也就是说,它将继续从'w'
开始打印字符,直到找到\0代码>。这就是world
的打印方式
就是这样,
const char *s="hello, world";
const char *pchar = &s[7]; //initialize pchar with the address of `w`
cout<< pchar <<endl; //prints all chars till it finds `\0`
但是,如果要打印s[7]
的地址,即&s[7]
的值,则必须执行以下操作:
cout << ((void*)&s[7]) << endl; //it prints the address of s[7]
cout您的标题与您的问题完全不匹配
由于s
是指向字符的指针(又名C样式字符串),s[7]
是字符。由于s[7]
是字符,&s[7]
是指向字符的指针(又名C样式字符串)。s[7]是字符。(您可以索引到指向数组的指针,就好像它只是数组的名称一样)
&s[7]是指向索引7处字符的指针。它的类型是char*,因此流插入器将其视为char*
world
cout << ((void*)&s[7]) << endl; //it prints the address of s[7]