带空终止符的指针数组交互

发布于 2024-11-30 09:21:12 字数 617 浏览 2 评论 0原文

我只是在处理数组时尝试使用指针,但我对 C++ 如何处理数组感到有点困惑。以下是我编写的相关代码:

//declare a string (as a pointer)
char* szString = "Randy";               

cout << "Display string using a pointer: ";
char* pszString = szString;
while (*pszString)
cout << *pszString++;

首先,当我尝试使用 cout 写入“pszString”中的内容(不取消引用)时,我有点惊讶地发现它给了我字符串。我只是假设这是因为我给了指针一个字符串而不是一个变量。

但真正引起我注意的是,当我从 cout << 行中删除星号时, *pszString++; 它打印了“Randandyndydyy”。我不知道为什么它会写入数组,然后再次写入少 1 个字母。我的推理是,在写入字符字符串后,增量运算符立即将索引带到下一个字母,然后才能到达空终止符。我不明白为什么空终止符不会导致循环在第一次输出字符串后返回 false。这是正确的推理吗?有人可以解释一下我是否得到了数组和指针之间的这种关系吗?

I was just experimenting with the use of pointers when dealing with arrays and I've become a bit confused with how C++ is handling the arrays. Here are the relevant bits of code I wrote:

//declare a string (as a pointer)
char* szString = "Randy";               

cout << "Display string using a pointer: ";
char* pszString = szString;
while (*pszString)
cout << *pszString++;

First off, when I tried using cout to write what was in "pszString" (without de-referencing)I was a bit surprised to see it gave me the string. I just assumed it was because I gave the pointer a string and not a variable.

What really caught my attention though is that when I removed the asterisk from the line cout << *pszString++; it printed "Randyandyndydyy". I'm not sure why it's writes the array AND then writes it again with 1 letter less. My reasoning is that after writing the char string the increment operator immediately brings the index to the next letter before it can reach the null terminator. I don't see why the null terminator wouldn't cause the loop to return false after the string is output for the first time otherwise. Is this the right reasoning? Could someone explain if I'm getting this relationship between arrays and pointers?

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

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

发布评论

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

评论(1

作死小能手 2024-12-07 09:21:12

cout 有一个 operator<< 重载,用于 char* 打印整个字符串(即打印每个字符,直到遇到 >0)。相比之下,coutoperator<<char 重载仅打印该一个字符。这就是本质上的区别。如果您需要更多解释,请继续阅读。

当您在递增指针后取消引用指针时,您将发送 cout 一个 charnotchar*,所以它打印一个字符。

所以cout << *pszString++; 就像做

cout << *pszString;
pszString = pszString + 1;

当您取消引用指针时,您将向其发送一个char*,因此cout 打印整个字符串,并且在循环的每次迭代中将字符串的开头向上移动一个字符。

所以cout << pszString++; 就像做

cout << pszString;
pszString = pszString + 1;

带有一个小循环展开的插图:

For cout << *pszString++;

Randy\0
^ pszString points here

// this means increment pszString and send cout the character at which pszString *used* to be pointing
cout << *pszString++;

// so cout prints R and pszString now points
Randy\0
 ^ here

// this means increment pszString and send cout the character at which pszString *used* to be pointing
cout << *pszString++;

// so cout prints a and pszString now points
Randy\0
  ^ here

// and so on

对于cout << pszString++;

Randy\0
^ pszString points here

// this means increment pszString and pass the old pointer to cout's operator<<
cout << pszString++;

// so cout prints Randy, and now pszString points
Randy\0
 ^ here

cout << pszString++;

// cout prints andy, and now pszString points
Randy\0
  ^ here

// and so on

我很高兴您以这种方式尝试指针,它会让您真正知道发生了什么,不像许多程序员会做任何事情来摆脱处理指针的麻烦。

cout has an operator<< overload for char* to print the entire string (that is, print each character until it encounters a 0). By contrast, the char overload for cout's operator<< prints just that one character. That's essentially the difference here. If you need more explanation, read on.

When you dereference the pointer after incrementing it, you're sending cout a char, not and char*, so it prints one character.

So cout << *pszString++; is like doing

cout << *pszString;
pszString = pszString + 1;

When you don't dereference the pointer, you're sending it a char* so cout prints the entire string, and you're moving the start of the string up by one character in each iteration through the loop.

So cout << pszString++; is like doing

cout << pszString;
pszString = pszString + 1;

Illustration with a little loop unrolling:

For cout << *pszString++;

Randy\0
^ pszString points here

// this means increment pszString and send cout the character at which pszString *used* to be pointing
cout << *pszString++;

// so cout prints R and pszString now points
Randy\0
 ^ here

// this means increment pszString and send cout the character at which pszString *used* to be pointing
cout << *pszString++;

// so cout prints a and pszString now points
Randy\0
  ^ here

// and so on

For cout << pszString++;

Randy\0
^ pszString points here

// this means increment pszString and pass the old pointer to cout's operator<<
cout << pszString++;

// so cout prints Randy, and now pszString points
Randy\0
 ^ here

cout << pszString++;

// cout prints andy, and now pszString points
Randy\0
  ^ here

// and so on

I am glad you are experimenting with pointers this way, it'll make you actually know what's going on unlike many programmers who will do anything to get away from having to deal with pointers.

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