简体   繁体   中英

Problem dereferencing char pointer in C++

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.

Try

cout << *cp << endl;

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.

Sadly, from its origins of the C programming language, char * (ie 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 (ie multiple contiguous) charactes, until it reaches a NUL character (ie numeric value 0, not the character '0' which has a numeric byte value of 48 or 0x30).

So, if in your code you have

char *sz = "123";

this will allocate 4 characters, the 3 you see plus the NUL character

Addr:0x00050014 0x31  // ASCII char '1'
Addr:0x00050015 0x32  // ASCII char '2'
Addr:0x00050016 0x33  // ASCII char '3'
Addr:0x00050017 0x00  // ASCII char nul

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 (ie 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++ (or, more specificaly, ostream in this case), will automatically treat a char* as a (C style) string. You need to explicitly cast it to the type you want, such as:

cout << (void*)cp << endl;

if you want the pointer value, or

cout << *cp << endl;

if you want the character itself (as your question seems to indicate).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM