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.
Related
This question already has answers here:
C - initialization of pointers, asterisk position [duplicate]
(9 answers)
Closed 8 years ago.
It might seems a noob question but I simply didnt find anywhere, and I'm curious about as a starter in C/C++. In what side I should put the asterisk? I've seen people using both declarations.
Nothing, though I prefer the latter version as I read * as "pointer".
So T* x; means x is a pointer to T.
Note that T* x, y; is the same as:
T* x;
T y;
Which is perhaps why some people recommend placing the * next to the variable name, but I personally just don't do multiple declarations on the same line because I think it's a bit ugly.
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 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.
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.
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.