You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

char指针与其他原生类型指针的cout输出行为差异问询

Why does a char pointer output characters (with garbage) instead of its address when printed directly in C++?

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:

  1. Special handling of char* by std::cout
    The C++ standard library has an overloaded version of operator<< for const 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 a char* directly with cout, it will keep outputting characters starting from the pointer's address until it hits a '\0' in memory.

    In your code, the test1 array is filled with 7 characters but has no '\0' at the end. So when you print t2 (which points to 'e'), cout outputs "etya" followed by whatever random bytes happen to be in memory until it finds a '\0'—that's where the garbage comes from.

  2. Why other pointer types behave differently
    For int*, float*, or std::string*, there's no such special overload in std::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 print v2, f2, or str2 directly.

  3. How to print the address of a char*
    If you want to output the actual memory address of your char* instead of treating it as a string, you have two easy options:

    • Cast the char* to void*:
      std::cout << static_cast<void*>(t2) << std::endl;
      
      std::cout will print the address of any void* pointer, since there's no special overload for it.
    • Use printf instead (which treats all pointers the same for address output):
      printf("%p\n", t2);
      
  4. Quick notes on your code

    • The main function in C++ should return int, not void. Change void main(...) to int main(...) and add return 0; at the end to comply with the standard.
    • The parameters of main are usually written as int argc, char* argv[] (you swapped the names argv and argc—this doesn't break the code, but it's confusing for other developers).

内容的提问来源于stack exchange,提问作者SpelingError

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:01:07