指针的两种不同类型的星号位置
Two different types of asterisk locations for a pointer?
假设你有这个。
int fry = 50;
现在 wen 做一个指针并引用值,星号可以有两种不同的放置方式:
int* d1 = &fry;
和。。。
int *d1 = &fry;
我想知道有什么区别以及最好使用每种情况?谢谢!
没有区别。你甚至可以做int*d1
和int * d1
.
在我看来,您应该更喜欢int *d1
:此选择在避免混淆方面特别有用:
int* d1, d2;
int *d1, d2;
这些语句都声明一个整数和一个指向整数的指针。第一个版本看起来很有误导性。
你给出的两个例子没有区别。至于你应该使用哪个,请遵循你正在使用的代码的风格。
但请注意,* 绑定到变量,而不是它们类型。因此
int *this_is_a_pointer, this_is_just_an_int;
和
int* this_is_a_pointer, this_is_just_an_int;
都是一样的。如果你需要这样的东西,我实际上会写以下任何一篇以避免任何混淆
int* this_is_a_pointer;
int this_is_just_an_int;
或
int *this_is_a_pointer;
int this_is_just_an_int;
取决于所使用的样式。
相关文章:
- 在C++中将函数压缩为两种方式
- 如何使映射键具有两种不同的数据类型?
- 两种访问I2C总线的方法有什么区别?
- 两种模板示例有什么区别?
- 如何构造可以调用和返回两种不同类型的模板
- 这两种C++语法之间有什么区别?
- 为什么两种不同的对象初始化方式给出不同的输出
- std::cin 从控制台获取两种不同的变量类型,'storing'以后使用第二种类型?
- 定义类模板构造函数的两种方法之间的区别
- 初始化类的两种方法?
- C++ 一个函数,可以根据接受的值返回两种类型之一
- 如何检查程序员在C++中提供的两种不同格式的输入
- C++指针中的这两种类型的值分配有什么区别?
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 有没有办法让C++函数采用具有相同成员的两种不同类型?
- C++两种类型相互依赖
- 两种情况下的输出不应该相同吗?
- C++:这两种将数字写入矩阵的方式之间是否存在显着的速度差异?
- 在没有堆的情况下用两种方法构造对象
- 指针的两种不同类型的星号位置