在指针声明中放置星号 [英] Placement of the asterisk in pointer declarations

查看:36
本文介绍了在指针声明中放置星号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近决定我最终只需要学习 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.

这些例子怎么样:

  1. int* test;
  2. int *test;
  3. int * test;
  4. int* test,test2;
  5. int *test,test2;
  6. 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;

这篇关于在指针声明中放置星号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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