如果 x 是指针,&x 与 x 有何不同?

发布于 2024-11-09 17:02:31 字数 363 浏览 3 评论 0原文

所以...我有以下代码:

int main(void)
{
const char *s="hello, world";
cout<<&s[7]<<endl;

return 0;
}

它打印“world”...但是我不明白为什么会这样:

int main(void)
{
const char *s="hello, world";
cout<<s[7]<<endl;

return 0;
}

只会打印“w”(我所做的只是去掉了&符号),但我认为是“地址”运算符......这让我想知道为什么你需要它以及它的功能是什么?

So...I have the following code:

int main(void)
{
const char *s="hello, world";
cout<<&s[7]<<endl;

return 0;
}

and it prints "world"...however I don't understand why this:

int main(void)
{
const char *s="hello, world";
cout<<s[7]<<endl;

return 0;
}

only will print "w" (all I changed is got rid of the ampersand), but I thought that was the "address of" operator...which makes me wonder why you need it and what it's function is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一紙繁鸢 2024-11-16 17:02:31

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` 

输出:

world

但是,如果你想打印s[7]的地址,即&s[7]的值,那么你必须这样做:

cout << ((void*)&s[7]) << endl; //it prints the address of s[7]

现在我们传递一个 void* 类型的地址,因此 cout 将无法推断出它用来推断 char* 的内容> 输入地址。 void* 隐藏有关地址内容的所有信息。因此,cout 只是打印地址本身。毕竟,这才是它最多得到的。

在线演示:http://www.ideone.com/BMhFy

s[7] is the character 'w', so &s[7] becomes the address of the character 'w'. And when you pass an address of char* type to cout, it prints all characters starting from the character to the null character which is end of the string i.e it continues to print characters from 'w' onward till it finds \0 . That is how world gets printed.

Its like this,

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` 

Output:

world

However, if you want to print the address of s[7], i.e value of &s[7], then you've to do this:

cout << ((void*)&s[7]) << endl; //it prints the address of s[7]

Now we pass an address of void* type, so cout wouldn't be able to infer what it used to infer with char* type address. void* hides all information regarding the content of the address. So cout simply prints the address itself. After all, that is what it gets at the most.

Online demonstration : http://www.ideone.com/BMhFy

盛夏已如深秋| 2024-11-16 17:02:31

你的标题根本不符合你的问题。

由于 s 是指向 char(又名 C 风格字符串)的指针,因此 s[7] 是一个 char。由于 s[7] 是一个 char,因此 &s[7] 是一个指向 char 的指针(也称为 C 风格字符串)。

Your title doesn't match your question... at all.

Since s is a pointer to char (aka C-style string), s[7] is a char. Since s[7] is a char, &s[7] is a pointer to char (aka C-style string).

弥枳 2024-11-16 17:02:31

s[7] 是一个字符。 (您可以对指向数组的指针进行索引,就好像它只是数组的名称一样)。

&s[7] 是指向索引 7 处的字符的指针。它的类型是 char*,因此流插入器将其视为 char*。

s[7] is a character. (You can index into pointers to an array as if it was just the name of the array).

&s[7] is a pointer to the character at index 7. it's type is char*, so the stream inserter treats it like a char*.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文