int* pointer vs. int *pointer for pointer declaration [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What makes more sense - char* string or char *string?
The syntax for pointers has never made sense to me. I noticed that both of these options work for declaring a pointer. Which one should I be using?

The whitespace is insigificant for the C compiler.
The difference matters more if you have multiple declarations on the same line:
int* p1, q1; // p1 is a pointer to int, q1 is an int.
int *p2, *q2; // p2 and q2 are both pointers to ints.
Putting the asterisk near to the variable name may help you to remember this. But it's probably better to just declare each variable on its own line.

It's a question of style, so you should probably use the same style (consistently) as others working on the same code. If it's just you, pick whichever you like.
Personally would argue for int *pointer because int* a, b; means the same as int *a; int b; not int *a; int *b; as one might think, but this is admittedly a fringe case.

The spacing is irrelevant for parsing. There are 3 tokens: int, *, and pointer.

They are equivalent, and a matter of personal preference.
For completeness, I would add that if you're declaring two pointers, you will have to write:
int *a, *b;
If you write int *a,b, you end up with one pointer to int and one int.

From Microsoft's All-In-One Code Framework Coding Standards:
You should put a space between the * character(s) and the type when specifying a pointer type/variable, but there should be no space between the '*' character(s) and the variable. Setting this rule is to be consistent and uniform in code.

You can also use
int * pointer
it just a matter of personal preference. The c++ compiler will know what you mean, just as long as you have the correct syntax.

There is no difference between the two. Use the one that you like more.

They are both equal. Choose the one you like the most, or follow the style guidelines if you are editing existing code.

Related

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

This question already has an answer here:
Difference between char* var; and char *var;? [duplicate]
(1 answer)
Closed 9 years ago.
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.
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
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).
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.
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.
There's absolutely no difference.
No difference. I've tended to prefer the
int* x
Due to thinking of 'x' as having type int* .
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
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.
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
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.

Where to put the star in C and C++ pointer notation [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Correct way of declaring pointer variables in C/C++
For some time the following has been annoying me, where should I put the star in my pointer notation.
int *var; // 1
and
int* var; // 2
obviously do the same thing, and both notations are correct, but I find that most literature and code I look at use the 1th notation.
wouldn't it be more 'correct' to use the 2th notation, separating the type and the variable name by a whitespace, rather than mixing the type and variable tokens?
No. Never. <g>
But consider:
int* var1, var2;
Here, the placement of the * is misleading, because it does not apply to var2, which is an int and not an int*.
The Linux kernel coding style convention is:
int *ptr1 , *ptr2;
So I think you should adopt it as your convention.
When declaring pointer data or a function that returns a pointer type, the
preferred use of * is adjacent to the data name or function name and not
adjacent to the type name. Examples:
char *linux_banner;
unsigned long long memparse(char *ptr, char **retptr);
char *match_strdup(substring_t *s);
I believe part of the reason for this notation is so that the usage and declaration of a variable look similar.
int *var;
int x;
x = *var;
You can also think of it as saying that dereferencing var will give you an int.

C++ pointer declaration [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C, what is the correct syntax for declaring pointers?
In C++ What is the difference between:
int* a;
and
int *a;
Is it same?
Yes. Those two constructs are identical. int *a; is more C style, because it is consistent with the "declaration follows use" rule in C. This rule means that you can read *a, and know that it gives you an int.
In C++, types get used on their own more often, so int* a; is more typical, as it puts the emphasis on they type being int*. Conformance to "declaration follows use" is less important in C++, because does not work everywhere anyway (it doesn't work with references, for example).
Note that if you write int* a, b; (which is the same as int *a, b;), then only a is a pointer.
They are the same. There is no difference. Also same as
int * a;
Those are the same. You can put the asterisk (*) anyplace.

Correct way of declaring pointer variables in C/C++ [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I noticed some people use the following notation for declaring pointer variables.
(a) char* p;
instead of
(b) char *p;
I use (b). What is the rational behind the notation (a)?
Notation (b) makes more sense to me because character pointer is not a type itself. Instead the type is character and the variable may be a pointer to the character.
char* c;
This looks like there is a type char* and the variable c is of that type. But in fact the type is char and *c (the memory location pointed by c) is of that type (char). If you declare multiple variables at once this distinction becomes obvious.
char* c, *d;
This looks weird. Both c and d are same kind of pointers that point to a character. In this since the next one looks more natural.
char *c, *d;
Bjarne Stroustrup 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.
Source: http://www.stroustrup.com/bs_faq2.html#whitespace
I'd recommend the latter style because in the situation where you are declaring multiple pointers in a single line (your 4th example), having the asterisk with the variable will be what you're used to.
I personally prefer to place the * with the rest of the type
char* p; // p is a pointer to a char.
People will argue "but then char* p, q; becomes misleading", to which I say, "so don't do that".
There are no difference how to write. But if you want to declare two or more pointers in one line better to use (b) variant, because it is clear what you want. Look below:
int *a;
int* b; // All is OK. `a` is pointer to int ant `b` is pointer to int
char *c, *d; // We declare two pointers to char. And we clearly see it.
char* e, f; // We declare pointer `e` and variable `f` of char type.
// Maybe here it is mistake, maybe not.
// Better way of course is use typedef:
typedef char* PCHAR;
PCHAR g, h; // Now `g` and `h` both are pointers.
// If we used define construction for PCHAR we'd get into problem too.
The compromise is
char * p;
K&R uses
char *p;
It's up to you unless you're following a coding standard -- in that case, you should follow what everyone else does.
It's all a matter of preference, personally on projects that I see the char* I tend to declare multiple pointers on separate lines. There's no real "correct" way to do this and it all comes down to preference. Some say it's easier to read (a) while others say that (b) is easier to declare more variables of the same type on the same line.
I find (b) to be more common, and in some cases I have seen
char * a;
or something like this. Again preference. Whatever you're comfortable with or whatever the project I'm working on uses I will use (unless I write it myself in which case I use (a))

C++ Pointer in Function

I know that technically all three ways below are valid, but is there any logical reason to do it one way or the other? I mean, lots of things in c++ are "technically valid" but that doesn't make them any less foolish.
int* someFunction(int* input)
{
// code
}
or
int *someFunction(int *input)
{
// code
}
or
int * someFunction(int * input)
{
// code
}
I personally think the third one is annoying, but is there a "correct" way? I am typically more inclined to use the first one (as the second looks more like it's being used as the dereference operator - which it isn't)
It's a question of personal taste. I prefer the 1st approach, whereas the old-school programmers tend to use the 2nd (coming from the old good C times).
For a difference, consider the following:
int* p, q; // here p is a pointer to int, but q is just an int!
The attractiveness of the first way (int* p) is that it reads as "int pointer is a type of p", whereas the alternate int *p reads as "int is a type of *p" (which is also correct).
All are equivalent. Choose the flavor that suits you best. Just be sure whichever you chose, you apply that choice in every case. Where your stars and curly braces go is far less important than putting them in the same place every time.
Personally, I prefer int* someFunction(int* input);, but who cares?
I personally use the second option, because int *p1, p2; is better looking and less confusing than int* p1, p2; or int * p1, p2;. The same with functions' return type to keep the same style.
It's a personal thing, there isn't any 'good' or 'bad' way.
All are equivalent. I like the third because it makes the * stand out. Other people differ.
If you're working on a project with others, use the established style. If not, decide on your own style.
There's no "one correct way". It is all a matter of personal preference.
The first approach is not generally compatible with having multiple declarators in one declaration, so people who use it usually don't use more than one declarator in a declaration
int* p, b; // misleading, since `b` is `int` and not `int*`
Yet the first approach has its supporters.
I prefer the second, for the reasons explained in my previous answer. *someFunction(someInput) has type int.
EDIT: Kernigan and Ritchie definitely intended the second. See for instance this snippet from the white bible.