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

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

问题描述

我最近决定,我只是终于学会C / C ++,并有一件事我真的不明白关于指针或更多precisely,它们的定义。

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 *测试;

  2. 为int *测试;

  3. 为int *测试;

  4. 为int *测试,测试2;

  5. 为int *测试,测试2;

  6. 为int *测试,测试2;

  1. int* test;
  2. int *test;
  3. int * test;
  4. int* test,test2;
  5. int *test,test2;
  6. int * test,test2;

现在,我的理解,前三种情况都做同样的:测试不是int,而是指向包含一个

Now, to my understanding, the first three cases are all doing the same: Test is not an int, but a pointer to one.

第二组的例子是有点更靠谱。在情况4,测试和测试2将指向一个int,而在5的情况下,唯一的测试是一个指针,而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是同样的事情,只有的测试的是一个指针。如果你想两个指针,你应该使用:

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天全站免登陆