我最近决定,我只需要最终学习C/C++,关于指针,或者更准确地说,它们的定义,有一件事我并不真正理解。
下面这些例子怎么样:
int* test;
int *test;
int * test;
int* test,test2;
int *test,test2;
int * test,test2;
现在,据我所知,前三种情况都在做同样的事情: Test不是一个int,而是一个指向int的指针。
第二组示例有点棘手。在案例4中,test和test2都是指向一个整数的指针,而在案例5中,只有test是一个指针,而test2是一个“真正的”整数。案例6呢?和案例5一样?
发布于 2008-10-07 21:04:33
4、5和6是一回事,只是test是一个指针。如果你想要两个指针,你应该使用:
int *test, *test2;
或者,更好的(把一切都弄清楚):
int* test;
int* test2;
发布于 2008-10-07 21:05:53
星号周围的空格没有任何意义。这三个意思都是一样的:
int* test;
int *test;
int * test;
"int *var1, var2
“是一种邪恶的语法,它只是为了迷惑人们,应该避免。它扩展到:
int *var1;
int var2;
发布于 2008-10-07 21:13:23
许多编码指南建议您在每行中只声明一个变量的。这就避免了你在问这个问题之前的那种困惑。与我共事过的大多数C++程序员似乎都坚持这样做。
我知道有点旁白,但我发现一些有用的东西是倒着读声明。
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
https://stackoverflow.com/questions/180401
复制相似问题