打印C ++ int指针与char指针 [英] Printing C++ int pointer vs char pointer

查看:262
本文介绍了打印C ++ int指针与char指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时:

int i[] = {1,2,3};
int* pointer = i;
cout << i << endl;

char c[] = {'a','b','c','\0'};
char* ptr = c;
cout << ptr << endl;

我得到以下输出:

0x28ff1c
abc

为什么int指针返回地址,而char指针返回数组的实际内容呢?

Why does the int pointer return the address while the char pointer returns the actual content of the array?

推荐答案

这是由于的重载造成的<< 运算符。对于 char * ,它将其解释为以null结尾的C字符串。对于 int 指针,您只需获取地址即可。

This is due to overload of << operator. For char * it interprets it as null terminated C string. For int pointer, you just get the address.

这篇关于打印C ++ int指针与char指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆