What is the difference between the following?
char* mystring;
char *mystring;
What is the difference between the following?
char* mystring;
char *mystring;
Nothing but semantics - ie how it looks.
http://www.research.att.com/~bs/bs_faq2.html#whitespace
I also assume there's no difference between char** mystring and char **mystring either right?
what about char* *mystring?
:P
Nope, same thing. To the compiler, it doesn't matter.
I tend to emphasize the type, so I write char***--etc.
You could also write char** * to emphasize that it is a pointer (the lone *) to char** (the type to the far left).
In the end, it's up to you.
I emphasize types because pointers are NOT the same as non-pointers, so assigning int* to int will cause complaints.
Thus I see that the pointer is of type char* and points to char (remove the *).
Also, many tend to see the * as part of the name, but in reality it is not, because "*" is not a valid character in a name, which is another reason I put it on the left.
As always, Thanks!
For a different view, here's my opinion:
Always put the "*" next to the name in a variable definition, i.e. use "char *foo" instead of "char* foo".
Why?
Consider the following code:
To me, this looks like foo and bar are both pointers to char, when in fact foo is a pointer to char and bar is not.Code:char* foo, bar;
On the other hand, the "*" modifies the type, so it may also belong to the type name, not the variable name. It's a matter of style and, most importantly, consistency.
Greets,
Philip
I see the what you mean Philip. Personally I always put one variable declaration per line.
char *foo;
char *bar;
mostly so I can put comments after each line so I know what the variables are for.
That being said I still always choose to use char *foo rather than char* bar.
Using pascal convention like multiline variable's type definition might be a little help to the code cleanliness.
Code:typedef int TInt; typedef TInt *PInt; struct ArrayOfInt { PInt array; TInt length; }; void add(PInt buffer){ }
Just GET it OFF out my mind!!