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

坚强是说给别人听的谎言 提交于 2019-12-03 16:53:51

问题


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.

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.

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.


回答1:


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

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.

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

From here




回答2:


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, y and z are all pointers, which they are not (only x is).

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 int because it's part of the 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).




回答3:


No difference at all.

It is just a matter of style.

I personally prefer 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 x is a pointer to int, but are y and z pointers too? It looks like they're pointers, but they are not.




回答4:


There is no difference. I use the int* x form 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].

Some people prefer int *x because you can read it as "dereferencing x gives you an int". But even that falls apart when you start to use reference types; int &x does not mean that taking the address of x will give you an int.

Another reason that you might prefer int *x is because, in terms of the grammar, the int is the declaration specifier sequence and the *x is 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 y is not a pointer in this case.

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* x form.

There's only one rule: be consistent.




回答5:


There's absolutely no difference.




回答6:


No difference. I've tended to prefer the

int* x

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




回答7:


To the compiler they are the same

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

int *x,y

However this is more misleading

int* x,y




回答8:


Both are the same thing.

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

int* p, q;

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

The correct syntax would be:

int* p, *q;

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




回答9:


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




回答10:


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



来源:https://stackoverflow.com/questions/15932925/is-there-a-difference-between-int-x-and-int-x-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!