本文介绍了字符*变种之间的差异;和char * VAR ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是否有任何区别:

Just wondering if there's any difference between:

char* var;
char *var;

或者是它只是一个preference(间距)?

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

推荐答案

有是在这种情况下,没有任何区别。然而,你应该preFER 的char *变种;

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 ,一个指针到 - 字符 B ,一个字符。这是令人困惑!因为 * 性格更接近字符关键字,我们预计类型的两个变量将是指针到 - 字符,而 * 实际上是相关联的只有的有一个。 (这类似于 char的一个​​[10],B; 在评论中指出由teppic;在 [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 是一个指针。从道格拉斯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;

这属于preY上面说明了同样的问题:如果 B 不是指针则声明(的char * A,B ; )也可能导致对程序员的意图的不确定性。因此,我建议不要把之间的空间 * 和变量名。

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 name.

你看它的任何方式,的char *变种; 应被视为不良作风。它的语法正确根据语言规范,但会导致无法读取code,因为它出现的 的,以表明 * 说明符的一部分键入在同一声明的所有变量共享的,当它不是。 (这是类似于写一个复杂的功能都在同一行 - 。它的工作原理,但它是一个坏主意,有哪里可读性和可维护性取代个人风格preferences案件,这是其中之一)

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.)

就个人而言,我preFER把一个空间后 * 只有的使用功能打交道时返回值:

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

char * foo();

这是明确的:它是返回一个指针到一个功能 - 字符

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

char *foo();

难道程序员意味着返回一个指针到一个功能 - 字符,还是他的意思是创建一个指向返回的函数?语言将解决这个作为等同于其他的原型,但我们的目标是可读性,这意味着准确传达我们的意思来的人类的,不仅仅是编译器。

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 ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 00:53