char* var 的区别;和 char *var;? [英] Difference between char* var; and char *var;?

查看:28
本文介绍了char* var 的区别;和 char *var;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道两者之间是否有任何区别:

Just wondering if there's any difference between:

char* var;
char *var;

还是只是喜好问题(间距)?

or is it just a matter of preference (spacing)?

推荐答案

在这种情况下没有区别.但是,您应该更喜欢 char *var;.

There is no difference in this case. However, you should prefer char *var;.

这是因为 * 与变量名称的关联更紧密,并且不是基本类型的一部分.例如,如果您这样做:

This is because the * is associated more closely with the variable name and is not part of the base type. For example, if you do this:

char* a, b;

你拥有的是a,一个指向char 的指针,以及b,一个char.这令人困惑!由于 * 字符更接近于 char 关键字,我们期望两个变量的类型都是指向 char 的指针,但是* 实际上onlya 相关联.(这类似于 teppic 在注释中指出的 char a[10], b;[10] 说明符同样仅与 a,所以只有 a 是一个数组.)

What you have is a, a pointer-to-char, and b, a char. This is confusing! Since the * character is closer to the char keyword, we expect that the types of both variables will be pointer-to-char, but the * is actually associated only with a. (This is similar to char a[10], b; as pointed out by teppic in the comments; the [10] specifier is likewise only associated with a, and so only a will be an array.)

正确的声明是:

char *a, *b;

* 说明符靠近变量意味着当您希望一个变量是指针而另一个不是时,很容易看出发生了什么:

Putting the * specifier closer to the variable means that it's easy to see what's going on when you intend for one variable to be a pointer and the other not:

char *a, b;

在这种情况下,很明显 b 不是一个指针.在原始示例 (char* a, b;) 中,我们不知道程序员是否打算将 b 用作指针.借用道格拉斯·克罗克福德 (Douglas Crockford) 的话,我们只知道程序员无能.

In this case it's obvious that b was not intended to be a pointer. In the original example (char* a, b;), we don't know whether or not the programmer intended for b to be a pointer. To borrow from Douglas Crockford, all we know is that the programmer is incompetent.

有些人喜欢在*前后加一个空格:

Some people like to put a space before and after the *:

char * a, * b;

这陷入了上面说明的相同问题:如果 b 不是指针,那么声明 (char * a, b;) 也可能导致不确定性程序员的意图.因此我建议不要在 * 和变量名1 之间放置空格.

This falls prey to the same problem illustrated above: if b is not a pointer then the declaration (char * a, b;) may also lead to uncertainty about the programmer's intent. Therefore I suggest not placing a space between the * and the variable name1.

无论你怎么看,char* var; 都应该被视为糟糕的风格.根据语言规范,它在语法上是正确的,但会导致代码不可读,因为它似乎表明 * 说明符是同一声明中所有变量共享的类型的一部分,当它不是.(这类似于在同一行上编写一个复杂的函数——它可以工作,但这是一个坏主意.在某些情况下,可读性和可维护性会取代个人风格偏好,这就是其中之一.)

Any way you look at it, char* var; should be treated as bad style. It's grammatically correct according to the language specification, but leads to unreadable code because it appears to indicate that the * specifier is part of the type shared by all variables in the same declaration, when it is not. (It's akin to writing a complex function all on the same line -- it works, but it's a bad idea. There are cases where readability and maintainability supersede personal stylistic preferences, and this is one of them.)

1我个人更喜欢在*后面加一个空格only在处理函数返回值时:

1Personally, I prefer to put a space after the * only when dealing with function return values:

char * foo();

这是明确的:它是一个返回指向char的指针的函数.

This is unambiguous: it's a function that returns a pointer-to-char.

char *foo();

程序员的意思是返回一个指向char 的指针的函数,还是他的意思是创建一个指向返回char 的函数的指针?该语言会将其解析为与其他原型相同,但我们的目标是可读性,这意味着向人类准确传达我们的意思,而不仅仅是编译器.

Did the programmer mean a function that returns a pointer-to-char, or did he mean to create a pointer to a function that returns char? The language will resolve this as identical to the other prototype, but our goal is readability, and that means conveying exactly what we mean to humans, not just the compiler.

这篇关于char* var 的区别;和 char *var;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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