在 C/C++ 中声明指针变量的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6990726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way of declaring pointer variables in C/C++
提问by keeda
I noticed some people use the following notation for declaring pointer variables.
我注意到有些人使用以下符号来声明指针变量。
(a) char* p;
instead of
代替
(b) char *p;
I use (b). What is the rational behind the notation (a)? Notation (b) makes more sense to me because character pointer is not a type itself. Instead the type is character and the variable may be a pointer to the character.
我使用(b)。符号(a)背后的理性是什么?符号 (b) 对我来说更有意义,因为字符指针本身不是一种类型。相反,类型是字符,变量可以是指向字符的指针。
char* c;
This looks like there is a type char* and the variable c is of that type. But in fact the type is char and *c (the memory location pointed by c) is of that type (char). If you declare multiple variables at once this distinction becomes obvious.
这看起来像是 char* 类型,而变量 c 就是该类型。但实际上类型是char,而*c(c指向的内存位置)是那种类型(char)。如果您一次声明多个变量,这种区别就很明显了。
char* c, *d;
This looks weird. Both c and d are same kind of pointers that point to a character. In this since the next one looks more natural.
这看起来很奇怪。c 和 d 都是指向一个字符的同一种指针。在这自下一个看起来更自然。
char *c, *d;
Thanks.
谢谢。
回答by BlackHyman
Bjarne Stroustrup said:
Bjarne Stroustrup 说:
The choice between "int* p;" and "int *p;" is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.
A "typical C programmer" writes "int *p;" and explains it "*p is what is the int" emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.
A "typical C++ programmer" writes "int* p;" and explains it "p is a pointer to an int" emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.
"int* p;" 之间的选择 和“int * p;” 不是关于对与错,而是关于风格和重点。C 强调表达式;声明通常被认为只不过是一种必要的罪恶。另一方面,C++ 非常重视类型。
“典型的 C 程序员”会写“int *p;” 并解释它“*p 是 int 是什么”强调语法,并可能指向 C(和 C++)声明语法来论证样式的正确性。实际上,* 绑定到语法中的名称 p。
“典型的 C++ 程序员”会写“int* p;” 并解释它“p 是一个指向 int 的指针”强调类型。实际上 p 的类型是 int*。我显然更喜欢这种强调,并且认为这对于很好地使用 C++ 的更高级部分很重要。
Source: http://www.stroustrup.com/bs_faq2.html#whitespace
来源:http: //www.stroustrup.com/bs_faq2.html#whitespace
I'd recommend the latter style because in the situation where you are declaring multiple pointers in a single line (your 4th example), having the asterisk with the variable will be what you're used to.
我推荐后一种风格,因为在您在一行中声明多个指针的情况下(您的第四个示例),将星号与变量一起使用将是您所习惯的。
回答by ikegami
I personally prefer to place the *
with the rest of the type
我个人更喜欢将*
与其余类型放在一起
char* p; // p is a pointer to a char.
People will argue "but then char* p, q;
becomes misleading", to which I say, "so don't do that".
人们会争论“但随后char* p, q;
会产生误导”,对此我说,“所以不要那样做”。
回答by George Gaál
There are no difference how to write. But if you want to declare two or more pointers in one line better to use (b) variant, because it is clear what you want. Look below:
怎么写没有区别。但是如果你想在一行中声明两个或更多的指针,最好使用 (b) 变体,因为你想要什么很清楚。往下看:
int *a;
int* b; // All is OK. `a` is pointer to int ant `b` is pointer to int
char *c, *d; // We declare two pointers to char. And we clearly see it.
char* e, f; // We declare pointer `e` and variable `f` of char type.
// Maybe here it is mistake, maybe not.
// Better way of course is use typedef:
typedef char* PCHAR;
PCHAR g, h; // Now `g` and `h` both are pointers.
// If we used define construction for PCHAR we'd get into problem too.
回答by jnnnnn
回答by Jesus Ramos
It's all a matter of preference, personally on projects that I see the char* I tend to declare multiple pointers on separate lines. There's no real "correct" way to do this and it all comes down to preference. Some say it's easier to read (a) while others say that (b) is easier to declare more variables of the same type on the same line.
这完全是一个偏好问题,就个人而言,在我看到 char* 的项目中,我倾向于在不同的行上声明多个指针。没有真正的“正确”方法可以做到这一点,这一切都取决于偏好。有人说 (a) 更容易阅读,而其他人则说 (b) 在同一行上声明更多相同类型的变量更容易。
I find (b) to be more common, and in some cases I have seen
我发现 (b) 更常见,并且在某些情况下我已经看到
char * a;
or something like this. Again preference. Whatever you're comfortable with or whatever the project I'm working on uses I will use (unless I write it myself in which case I use (a))
或类似的东西。再次偏好。无论你喜欢什么,或者我正在使用的任何项目,我都会使用(除非我自己写,在这种情况下我使用(a))