字符指针和整数指针之间的区别 [英] Difference between character pointer and integer pointer
本文介绍了字符指针和整数指针之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我们声明:
Hi,
If we declare:
int i;
int *ptr1 = &i;
*ptr1=10;
cout << ptr1;
ptr1
将给出地址.
但是:
Here ptr1
will give the address.
But:
char *ptr2;
ptr2="Priyaa";
cout << ptr2;
这里将给出字符指针的内容.
为什么会有这样的区别?
Here it will give the content of the character pointer.
Why is there such a difference?
推荐答案
因为char *是在C中定义字符串的唯一方法,并且在C ++中通常用于此目的.因此,它旨在以一种期望的方式处理char *.
Because a char * is the only way to define a string in C, and is commonly used for that purpose in C++. So, it''s designed to deal with a char * in the manner that it expects you''d want to deal with it.
cout << (int *)
和cout << (char *)
调用ostream::operator <<
的不同方法,因为它们必须处理不同的参数类型.
operator <<
有几种重载方法.如果您希望具有不同的行为,请自己为要更改的类型重载operater <<
.
cout << (int *)
andcout << (char *)
call different methods of theostream::operator <<
because they have to handle different parameter types.
There are several overloaded methods of theoperator <<
. If you want to have a different behaviour, overload theoperater <<
yourself for the types you want to change.
不同之处在于两个重载的实现<< .
:)
The difference is in the implementations of the two overloads of the ostream operator <<.
:)
这篇关于字符指针和整数指针之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文