在 C++ 中取消引用 char 指针时出现问题
我有一个小学校作业。我的输出不正确。你能看出我做错了什么吗?
//1. Create a char pointer named cp
char *cp;
//2. Dynamically allocate a char and store its address in cp
char dynamicChar = 'A';
cp = &dynamicChar;
//3. Write the character 'x' into the char
*cp = 'x';
//4. Print out the value of the char using cout
cout << cp << endl;
打印输出语句打印 A@@
而不仅仅是 A
。我不确定我做错了什么。
I have a small school assignment. My output is not correct. Can you see if I'm doing anything wrong?
//1. Create a char pointer named cp
char *cp;
//2. Dynamically allocate a char and store its address in cp
char dynamicChar = 'A';
cp = &dynamicChar;
//3. Write the character 'x' into the char
*cp = 'x';
//4. Print out the value of the char using cout
cout << cp << endl;
The print out statement prints A@@
instead of just A
. I'm not sure what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
您想要打印字符,而不是指向它的指针。当您将指针交给它时,
cout
认为您是在告诉它从此处开始打印一个字符数组。Try
You want to print the character, not the pointer to it. When you hand it the pointer,
cout
thinks you're telling it to print out a character array starting there.遗憾的是,从 C 编程语言的起源开始,char *(即指向字符的指针)并没有被视为只是指向像 int *(指向 int 的指针)或 float *(指向 float 的指针)这样的类型的指针,但许多库函数将它们视为 NUL 字符终止的字符串。我们所说的“字符串”是指文本。
因此,有很多库函数将 char* 视为特殊情况,并将它们视为“以 nul 结尾的字符串”,这意味着它将将该指针视为(即多个连续)字符的数组,直到它到达 NUL 字符(即数值 0,而不是数字字节值为 48 或 0x30 的字符“0”)。
因此,如果在您的代码中,您
将分配 4 个字符,则您看到的 3 个字符加上 NUL 字符
sz(指向字符的指针,表示地址)的值为 0x00050014(请注意,起始地址值由您的计算机通过多个步骤通过编译器,然后是链接器,然后是加载器,然后可能是程序本身)。
当你这样做时
<代码>cout << sz <<结束;
该程序会将指向字符的指针解释为指向以 null 结尾的字符缓冲区的指针,并转储字符串
123
到输出流 cout。它查看地址 0x00050014 处的值,并查看其是否为 NUL(即 0),如果不是,则转储该字符,然后查看下一个地址。如果 0x00050015 不是 NUL,则会转储它。然后 0x00050016 处的值发现它不是 NUL,将其转储出来,然后到达 0x00050017 并发现它是 NUL,并且不会将其转储出来,并停止查看地址并返回到您执行操作后的代码sz 的输出,特别是在这段代码中,它将返回到将 endl(行尾)转储到 cout 的点;
您最终将学习比旧的 C 风格 char* 机制更好的表示文本的方法,但现在,您只需要了解现有的遗留行为。
Sadly, from its origins of the C programming language, char * (i.e. pointers to characters) are not treated as just a pointer to a type like int * (pointer to int) or float * (pointer to a float), but many library functions treat these as NUL-character terminated strings. By "string" we mean text.
Therefore, there are lots of library functions that treat char* as special case and treats them as a "nul-terminated string", meaning it's gonna treat that pointer as an ARRAY of (i.e. multiple contiguous) charactes, until it reaches a NUL character (i.e. numeric value 0, not the character '0' which has a numeric byte value of 48 or 0x30).
So, if in your code you have
this will allocate 4 characters, the 3 you see plus the NUL character
sz (a pointer to a character, representing an address) will have the value 0x00050014 (note that the starting address value is determined by your computer via multiple steps via the compiler, then the linker, then the loader, then possibly the progrma itself).
When you do
cout << sz << endl;
the program will interpret the pointer to a character as a pointer to a nul-terminated buffer of characters and dump the character string
123
to the output stream cout. It looks at the value at address 0x00050014 and sees if its NUL (i.e. 0), if not, it dumps out the character, then looks at the next address. If 0x00050015 is not a NUL, it dumps IT out. Then the value at 0x00050016, sees its not a NUL, dumps it out, then it gets to 0x00050017 and sees that it IS NUL, and does NOT dump it out, and stops looking at addresses and returns to YOUR code just after where you did the output of sz, specifically in this code it will return to the point where it will dump the endl (end of line) to cout;
You will eventually learn better ways to represent text than the old C-style char* mechanism, but for now, you just need to understand the legacy behavior that exists.
C++(或更具体地说,在本例中为
ostream
)会自动将char*
视为(C 风格)字符串。您需要将其显式转换为您想要的类型,例如:如果您想要指针值,或者
如果您想要字符本身(正如您的问题似乎表明的那样)。
C++ (or, more specificaly,
ostream
in this case), will automatically treat achar*
as a (C style) string. You need to explicitly cast it to the type you want, such as:if you want the pointer value, or
if you want the character itself (as your question seems to indicate).