c ++:指针声明中星号的位置 以及 *符号与&符号的意思

这些示例如何:

int* test;
int *test;
int * test;

int* test,test2;
int *test,test2;
int * test,test2;

前三个案例都在做同样的事情:Test不是一个int,而是一个指向int的指针。

第二组,在情况4中,test和test2都是指向int的指针,而在情况5中,只有test是一个指针,而test2是"真实" int。 情况6呢? 与情况5相同?

4、5和6是同一件事,只是测试是一个指针。如果要两个指针,则应使用:

int *test, *test2;

或者,甚至更好(使一切清晰):

int* test;
int* test2;

星号周围的空白不重要。这三个意思相同:

int* test;
int *test;
int * test;

喜欢哪种样式都没有关系,并且每种有不同的解释方式,
第一种,testint*类型的,一个int指针
第二种,*testint类型的,指针指向的是int类型

" int *var1, var2"是一种邪恶的语法,使人感到困惑,应避免使用。它扩展为:

int *var1;
int var2;

有用的是向后读取声明。

int* test;   // test is a pointer to an int

当开始声明const指针时,要弄清是指针是const还是指针所指向的东西是const变得很棘手。

int* const test; // test is a const pointer to an int
int const * test; // test is a pointer to a const int ... but many people write this as  
const int * test; // test is a pointer to an int that's const

附加:

c++指针、*符号与&符号的意思

&a:存a的值的地方
*a:指向某个地址

*p=&a 指针p现在指向存a的值的地址
因此p的值=a的值
但是&p不等于&a,存p的值的地方是另一个地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值