Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 请问,;如果x是指针,那么x与x不同?_C++_Pointers_Ampersand - Fatal编程技术网

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=“你好,世界”;

cout
s[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]