char指针与其他原生类型指针的cout输出行为差异问询
Question:
I wrote a C++ program and noticed that the output of char pointers doesn't match my expectations:
#include <iostream> #include <stdlib.h> #include <string> void main(int argv, char* argc[]) { char* test1 = new char[7]; test1[0] = 'H'; test1[1] = 'a'; test1[2] = 'l'; test1[3] = 'e'; test1[4] = 't'; test1[5] = 'y'; test1[6] = 'a'; char* t2 = &test1[3]; std::cout << *t2 << std::endl; std::cout << t2 << std::endl; std::cout << std::endl; int v[] = { 1,2,3,4 }; int* v2 = &v[0]; std::cout << *v2 << std::endl; std::cout << v2 << std::endl; std::cout << std::endl; float f[] = { 0.2, 0.4, 0.6 }; float* f2 = &f[1]; std::cout << *f2 << std::endl; std::cout << f2 << std::endl; std::cout << std::endl; std::string str = "A_string"; std::string *str2 = &str; std::cout << *str2 << std::endl; std::cout << str2 << std::endl; std::cout << std::endl; std::cin.ignore(); }
The output is inconsistent: int, float, and string pointers print their addresses when output directly, and their values when dereferenced. But for char pointers, dereferencing works as expected, but direct output prints characters starting from the pointed position plus random garbage. I've tried different char array initialization methods and got the same result. Is this due to char type characteristics or an environment issue?
Answer:
This is definitely a feature of C++ (inherited from C), not an environment problem! Let me break it down for you:
Special handling of
char*bystd::cout
The C++ standard library has an overloaded version ofoperator<<forconst char*that treats the pointer as a C-style string. C-style strings rely on a null terminator ('\0') to mark the end of the string. When you print achar*directly withcout, it will keep outputting characters starting from the pointer's address until it hits a'\0'in memory.In your code, the
test1array is filled with 7 characters but has no'\0'at the end. So when you printt2(which points to'e'),coutoutputs "etya" followed by whatever random bytes happen to be in memory until it finds a'\0'—that's where the garbage comes from.Why other pointer types behave differently
Forint*,float*, orstd::string*, there's no such special overload instd::cout. The default behavior for these pointer types is to print their memory address in hexadecimal format. That's why you see addresses when you printv2,f2, orstr2directly.How to print the address of a
char*
If you want to output the actual memory address of yourchar*instead of treating it as a string, you have two easy options:- Cast the
char*tovoid*:std::cout << static_cast<void*>(t2) << std::endl;std::coutwill print the address of anyvoid*pointer, since there's no special overload for it. - Use
printfinstead (which treats all pointers the same for address output):printf("%p\n", t2);
- Cast the
Quick notes on your code
- The
mainfunction in C++ should returnint, notvoid. Changevoid main(...)toint main(...)and addreturn 0;at the end to comply with the standard. - The parameters of
mainare usually written asint argc, char* argv[](you swapped the namesargvandargc—this doesn't break the code, but it's confusing for other developers).
- The
内容的提问来源于stack exchange,提问作者SpelingError

