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.
Related
This question already has answers here:
Declaring multiple object pointers on one line causes compiler error
(5 answers)
Closed 5 years ago.
The type of left and right is node* so like other types they should be declared as node* left,right; Example int a,b;
You have to pick one way or the other, and this way is less awful. For an example of how awful the other way would be, compare:
int a[10], b[20], c[30];
to
int[10] a;
int[20] b;
int[30] c;
When programming in C, if you get in the habit of attaching the * to the variable names it will make more sense to you.
int *i,j;
i is an int* and j is an int.
This question already has answers here:
Difference Between Reference Sign After Data Type or Before Variable Name?
(3 answers)
Closed 7 years ago.
I'm trying to learn the basics of C++. The book I'm reading uses the following syntax:
func(int& x);
On the internet, I mostly see the following syntax:
func(int &x);
Both seem to do exactly the same. Is there any difference?
Literally no difference. Just a stylistic preference. The same is true of where you put the pointer.
See the answer to the Is int* p; right or is int *p; right? FAQ on Bjarne Stroustrup's website. It's for pointers, but what he writes equally holds for references:
Both are "right" in the sense that both are valid C and C++ and both
have exactly the same meaning. [...] The choice between int* p; and int *p; is not about right and wrong, but about style and emphasis.
I prefer int& x. This way the reference becomes part of the type, it's an "int reference" called x, instead of "int, reference of x"
I find that thinking of "int reference" as a discrete type makes it less confusing, but to the compiler it's the same thing.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
Pointer vs. Reference
To do a call-by-reference in C++, I think I could use either of those two:
int f(int *x);
int g(int &x);
They would be called like so:
int *w;
f(w);
g(*w);
int y;
f(&y);
g(y);
Is there a difference in the functions f and g? I should be able to work with x as an int* pointer and *x as an int inside both functions. So what is the difference?
Most of this is a matter of taste. There is one important difference though. A pointer (*) can have a null value whereas a reference cannot be null and always must refer to a valid object.
The reference can't be NULL, so you don't have to check that. Otherwise, it's probably just syntactic sugar (at least for simple use cases). Check the disassembly of your program to see.
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.
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))