为什么人们使用 Type *var 而不是 Type* var?

发布于 2024-12-14 08:26:01 字数 649 浏览 2 评论 0原文

可能的重复:
声明指针;星号位于类型和名称之间的空格左侧还是右侧?
我放置的位置有关系吗在 C++ 中声明指针时使用星号?

我搜索了,但我的关键字似乎不适合。我很确定这个问题已经被问过。所以,如果您知道链接或其他内容,请给我指出某个方向!

我的问题是,为什么这么多人使用

TypeIdentifier *varname;

而不是

TypeIdentifier* varname;

对我来说更符合逻辑,因为 * 修改类型而不是变量名称。

Possible Duplicate:
Declaring pointers; asterisk on the left or right of the space between the type and name?
Does it matter where I place the asterisk when declaring pointers in C++?

I searched, but my keywords don't seem to fit. I'm pretty sure this question has already been asked., so point me in some direction if you know a link or something !

My question is, why do so many people use

TypeIdentifier *varname;

Instead of

TypeIdentifier* varname;

which is much more logical to me as the * modifies the type and not the variable name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最好是你 2024-12-21 08:26:02

这是因为 C 的声明语法。

int* a, b;

看起来好像定义了两个 int* 类型的变量,但实际上将 a 声明为 int*b 类型作为 int 类型。如果你写

int *a, b;

这个就很明显了。然而,我个人更喜欢不写这样的定义,而是写这样的定义

int* a;
int b;

,这样不会对类型产生任何疑问。

That's because of C's declaration syntax.

int* a, b;

looks as if it defined two variables of type int*, but actually declares a as type int* and b as type int. If you write

int *a, b;

this is obvious. I personally however prefer to just not write such a definition, but instead write

int* a;
int b;

which leaves no doubt about the types.

混吃等死 2024-12-21 08:26:02

嗯,语言的语法却另有说法。您可以通过

Type * var1, var2;

这里会发生什么来轻松检查它?

Well, the syntax of language says otherwise. You can easily check it by

Type * var1, var2;

What will happen here?

记忆で 2024-12-21 08:26:02

C 约定是将 * 附加到变量上,因为一行中的多个变量不共享 *

int *a, b; /* a is an int*, while b is an int. */

在 C++ 中,规则是相同的,但是它由于上述声明的混乱性质,以及随后每个声明仅声明一个变量的做法,将疣附加到类型上更为常见:

int* a;
int b;

我倾向于 C++ 约定,但也允许多个非指针或-一行引用声明:

int a, b;
int* c;
int* d;

The C convention is to attach the * to the variable, because multiple variables in a row don't share the *:

int *a, b; /* a is an int*, while b is an int. */

In C++, the rules are the same, but it is more common to attach the wart to the type, due to the confusing nature of the above declaration, and the consequent practice of declaring only one variable per declaration:

int* a;
int b;

I lean towards the C++ convention, but also allowing multiple non-pointer-or-reference declarations on one line:

int a, b;
int* c;
int* d;
生生漫 2024-12-21 08:26:02

因为 K&R 使用了 Foo *bar 并且人们坚持使用它。

Because K&R used Foo *bar and people stuck with it.

尾戒 2024-12-21 08:26:01

在 C 和 C++ 中,声明语法(大部分)遵循用法。

该声明

int *n;

可以读作“n is of type int*”,或“*n is of type int”。如果你看一下语言语法,就会发现后者更接近于它的解析方式。

对于仅由名称(例如 int)和 * 组成的类型,它不会产生很大的差异,但对于更复杂的声明来说很重要。

通常的趋势是在 C 中使用 int *n;,在 C++ 中使用 int* n;。后者是因为这是 Stroustrup 的个人偏好。我认为他的观点是“n is of type int*”读取更自然,并且可以通过不编写复杂的声明来避免复杂性。例如,无论您喜欢哪种间距,最好

int *a;
int b;

int* a, b;

写成

int *a, b;

In both C and C++, the declaration syntax (mostly) follows usage.

The declaration

int *n;

can be read as "n is of type int*", or as "*n is of type int". And if you take a look at the language grammar, the latter corresponds more closely to the way it's parsed.

For a type consisting of just a name (like int) and a *, it doesn't make a whole lot of difference, but it matters for more complex declarations.

The usual tendency is to use int *n; in C and int* n; in C++. The latter is because that's Stroustrup's personal preference. His point, I think, is that the "n is of type int*" reading is more natural, and the complexities can be avoided by not writing complex declarations. For example, whichever spacing you prefer, it's better to write

int *a;
int b;

than either

int* a, b;

or

int *a, b;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文