Placement of the asterisk in pointer declarations(在指针声明中放置星号)
问题描述
我最近决定我最终只需要学习 C/C++,关于指针,或者更准确地说,我不太了解它们的定义.
I've recently decided that I just have to finally learn C/C++, and there is one thing I do not really understand about pointers or more precisely, their definition.
这些例子怎么样:
int* test;
int *test;
int * test;
int* test,test2;
int *test,test2;
int * test,test2;
现在,据我所知,前三种情况都在做同样的事情:Test 不是一个 int,而是一个指向 1 的指针.
Now, to my understanding, the first three cases are all doing the same: Test is not an int, but a pointer to one.
第二组例子有点棘手.在情况 4 中,test 和 test2 都是指向 int 的指针,而在情况 5 中,只有 test 是一个指针,而 test2 是一个真正的"int.案例6呢?和案例5一样吗?
The second set of examples is a bit more tricky. In case 4, both test and test2 will be pointers to an int, whereas in case 5, only test is a pointer, whereas test2 is a "real" int. What about case 6? Same as case 5?
推荐答案
4, 5, 6 是同一个东西,只有 test 是一个指针.如果你想要两个指针,你应该使用:
4, 5, and 6 are the same thing, only test is a pointer. If you want two pointers, you should use:
int *test, *test2;
或者,甚至更好(让一切都清楚):
Or, even better (to make everything clear):
int* test;
int* test2;
这篇关于在指针声明中放置星号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在指针声明中放置星号


基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01