C++ 中 int *x 和 int* x 之间有区别吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15932925/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:54:56  来源:igfitidea点击:

Is there a difference between int *x and int* x in C++?

c++variablespointersnaming-conventionsdeclaration

提问by Adam McKeown

I'm getting back into my C++ studies, and really trying to understand the basics. Pointers have always given me trouble, and I want to make sure I really get it before I carry on and get confused down the road.

我正在重新开始我的 C++ 研究,并真正尝试了解基础知识。指针总是给我带来麻烦,我想确保在我继续前进并在路上感到困惑之前我真的得到了它。

Sadly, I've been stymied at the very outset by an inconsistency in the tutorials I'm reading. Some do pointer declarations this way:

可悲的是,我从一开始就被我正在阅读的教程中的不一致所困扰。有些人以这种方式进行指针声明:

int *x

and some do it this way:

有些人这样做:

int* x

Now the former of the two seems to be by far the most common. Which is upsetting, because the second makes much more sense to me. When I read int *x, I read "Here is an int, and its name is *x", which isn't quite right. But when I read the second, I see "here is an int pointer, and its name is x", which is pretty accurate.

现在看来,两者中的前者似乎是最常见的。这令人沮丧,因为第二个对我来说更有意义。当我读到 int *x 时,我读到“Here is an int,and its name is *x”,这不太正确。但是当我读到第二个时,我看到“这里是一个 int 指针,它的名字是 x”,这是非常准确的。

So, before I build my own mental map, is there a functional difference between the two? Am I going to be a leper outcast if I do it the second way?

那么,在我构建自己的思维导图之前,两者之间是否存在功能差异?如果我采用第二种方式,我会成为麻风病人的弃儿吗?

Thanks.

谢谢。

回答by Thanakron Tandavas

The famous Bjarne Stroustrup, notable for the creation and the development of the C++, said ...

著名的Bjarne Stroustrup以 C++ 的创造和发展而闻名,他说...

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++ 的更高级部分很重要。

So, there's no difference. It is just a code style.

所以,没什么区别。这只是一种代码风格。

From here

这里

回答by NPE

To the compiler, they have exactly the same meaning.

对于编译器来说,它们具有完全相同的含义。

Stylistically, there are arguments for and against both.

在风格上,两者都有支持和反对的论据。

One argument is that the first version is preferable because the second version:

一个论点是第一个版本更可取,因为第二个版本:

int* x, y, z;

implies that x, yand zare all pointers, which they are not (only xis).

暗示x,yz都是指针,它们不是(仅x是)。

The first version does not have this problem:

第一个版本没有这个问题:

int *x, y, z;

In other words, since the *binds to the variable name and not the type, it makes sense to place it right next to the variable name.

换句话说,因为*绑定到变量名而不是类型,所以将它放在变量名旁边是有意义的。

The counter-argument is that one shouldn't mix pointer and non-pointer type in the same declaration. If you don't, the above argument doesn't apply and it makes sense to place the *right next to intbecause it's part of the type.

反对意见是不应在同一声明中混合指针和非指针类型。如果不这样做,则上述参数不适用,将*right放在旁边是有意义的,int因为它是 type 的一部分

Whichever school of thought you decide to subscribe to, you'll encounter both styles in the wild, and more (some people write int * x).

无论您决定接受哪种思想流派,您都会在野外遇到这两种风格,甚至更多(有些人写道int * x)。

回答by Nawaz

No difference at all.

完全没有区别。

It is just a matter of style.

这只是风格问题。

I personallyprefer this:

个人更喜欢这个:

int *x;

over this,

对此,

int* x;

Because the latter is less readable when you declare many variables on the same line. For example, see this:

因为当您在同一行上声明多个变量时,后者的可读性较差。例如,看这个:

int* x, y, z;

Here xis a pointer to int, but are yand zpointers too? It looks likethey're pointers, but they are not.

x是一个指向 的指针int,但也是yz指针吗?它看起来像他们三分球,但他们不是

回答by Joseph Mansfield

There is no difference. I use the int* xform because I prefer to keep all of the type grouped together away from the name, but that kind of falls apart with more complex types like int (*x)[10].

没有区别。我使用int* x表单是因为我更喜欢将所有类型组合在一起,远离名称,但这种类型会因更复杂的类型而分崩离析,例如int (*x)[10].

Some people prefer int *xbecause you can read it as "dereferencing xgives you an int". But even that falls apart when you start to use reference types; int &xdoes not mean that taking the address of xwill give you an int.

有些人更喜欢,int *x因为您可以将其读作“取消引用x给您一个int”。但是当您开始使用引用类型时,即使这样也会分崩离析;int &x并不意味着采用 的地址x会给您一个int.

Another reason that you might prefer int *xis because, in terms of the grammar, the intis the declaration specifier sequence and the *xis the declarator. They are two separate parts of the declaration. This becomes more obvious when you have multiple declarators like int *x, y;. It might be easier to see that yis not a pointer in this case.

您可能更喜欢的另一个原因int *x是,就语法而言,the intis 声明说明符序列和 the *xis声明符。它们是声明的两个独立部分。当您有多个声明符时,这变得更加明显int *x, y;y在这种情况下可能更容易看出它不是指针。

However, many people, like myself, prefer not to declare multiple variables in a single declaration; there isn't exactly much need to in C++. That might be another reason for preferring the int* xform.

然而,很多人,像我一样,不喜欢在一个声明中声明多个变量;在 C++ 中完全不需要。这可能是更喜欢这种int* x形式的另一个原因。

There's only one rule: be consistent.

只有一个规则:保持一致。

回答by Ian Lee

No difference. I've tended to prefer the

没有不同。我倾向于更喜欢

int* x

Due to thinking of 'x' as having type int* .

由于认为 'x' 具有类型 int* 。

回答by Ian Lee

There's absolutely no difference.

完全没有区别。

回答by Pradheep

To the compiler they are the same

对于编译器,它们是相同的

But at the same time the difference is readabilty when there are multiple variables

但同时,当有多个变量时,区​​别在于可读性

int *x,y

整数 *x,y

However this is more misleading

然而,这更具误导性

int* x,y

整数* x,y

回答by sicko

Both are the same thing.

两者都是一回事。

The former is although preferred. Suppose you want to declare 2 pointers p & q in the same line.

虽然前者是优选的。假设您想在同一行中声明 2 个指针 p & q。

int* p, q;

int* p, q;

This actually means 'p' is a pointer to an int, while 'q' is an int.

这实际上意味着 'p' 是一个指向 int 的指针,而 'q' 是一个 int。

The correct syntax would be:

正确的语法是:

int* p, *q;

int* p, *q;

Hence, we include the * in the pointer's name.

因此,我们在指针名称中包含 *。

回答by Arun Chandran C

This is a type of style of coding. There's no difference, just a matter of preference as you said yourself.Hope you clear.Enjoy coding

这是一种编码风格。没有区别,只是你自己说的喜好问题。希望你清楚。享受编码

回答by Box

In my opinion, in the expression int * x,y;xand yare names of variables. When these names are declared like this by programmer, it means they have same typeand int *is the type.

在我看来,在表达式中int * x,y;xy是变量的名称。当这些名称被程序员这样声明时,就意味着它们具有same type并且int *是类型。