带空终止符的指针数组交互
我只是在处理数组时尝试使用指针,但我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cout
有一个operator<<
重载,用于char*
打印整个字符串(即打印每个字符,直到遇到>0
)。相比之下,cout
的operator<<
的char
重载仅打印该一个字符。这就是本质上的区别。如果您需要更多解释,请继续阅读。当您在递增指针后取消引用指针时,您将发送
cout
一个char
、not 和char*
,所以它打印一个字符。所以
cout << *pszString++;
就像做当您不取消引用指针时,您将向其发送一个
char*
,因此cout
打印整个字符串,并且在循环的每次迭代中将字符串的开头向上移动一个字符。所以
cout << pszString++;
就像做带有一个小循环展开的插图:
For
cout << *pszString++;
对于
cout << pszString++;
我很高兴您以这种方式尝试指针,它会让您真正知道发生了什么,不像许多程序员会做任何事情来摆脱处理指针的麻烦。
cout
has anoperator<<
overload forchar*
to print the entire string (that is, print each character until it encounters a0
). By contrast, thechar
overload forcout
'soperator<<
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
achar
, not andchar*
, so it prints one character.So
cout << *pszString++;
is like doingWhen you don't dereference the pointer, you're sending it a
char*
socout
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 doingIllustration with a little loop unrolling:
For
cout << *pszString++;
For
cout << pszString++;
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.