如何打印char阵列的地址

How to print the address of char array

本文关键字:char 阵列 地址 打印 何打印      更新时间:2023-10-16

http://ideone.com/4p1gqr

#include <iostream>    
int main(int argc, char** argv)
{
  float *f = new float[10];
  std::cout << f << std::endl;
  std::cout << f + 3 << std::endl;
  char *c = new char[10];
  std::cout << c << std::endl;       // no print
  std::cout << c + 3 << std::endl;   // no print
  return 0;
}
stdout 
0x2b3cbaf1bc20
0x2b3cbaf1bc2c

如何打印char数组的地址?

您需要施放到void*以调用operator <<的正确超载,而不是输出为C字符串

std::cout << static_cast<void*>(c) << std::endl;
std::cout << static_cast<void*>(c+3) << std::endl;