What's the difference between char* a and char *a

What's the difference between char* a and char *a
 
  char*    a

 
  char    *a

 
  char  *  a
There is none. The compiler ignores whitespace.
from a video i watched the instructor said that
char* a
is incorrect format from another language, but c allows it anyways,
and that the proper way is
char *a
but in the end it doesnt matter.
Last edited on
char* a, b, c;

Only a is a pointer, but the spacing is misleading.

Consider instead:

char *a, b, c;

It is the same as before, but the spacing makes it more clear that only a is a pointer.
I see...thanks ^_^..
In C# and in C++ the meaning of declaration

char* a, b, c:

is different.

In C# declaration

char* a, b, c;

means that all three identifiesr have type char *.
While in C++ and C this declaration means that only identifier a has type char *.

So never use such records as for example char* a; or char& a; in C/C++. This only confuses the reader of the code and simply is a bad style of programming. In the West many programmers have no own brain so they simply repeat constructions by some idiot who is more experienced than they are.
Last edited on
vlad from moscow wrote:
In the West many programmers have no own brain

Well, this explains a lot
Last edited on
There are more zombies in the West, you see. More programmers have their brains eaten.
There are more zombies in the West, you see.
True so. Snowden and Assange just discoverd what that means...
@CharChen

One way to avoid confusion, is to only declare one variable per line of code. This way there is no confusion over whether it is a pointer or reference or not.

Then you can also comment what it means (even better provide a meaningful name in the first place), and / or comment other useful information like it's range of acceptable values say, or anything else that will be helpful.

Also, I am not sure what happens (because I never do it, and consequently not looked it up in the standard) when one puts a qualifier like short or unsigned before a list of variable declarations? Is the first one a short, and the rest int's? Does the same thing apply to const & static or any other qualifier? I have a sneaking suspicion that it does.

So avoid all this, and make it crystal clear by doing one declaration per line.

Hope all is well at your end :-)
Actually, even in reference, variables are declared like this:

char* a;

instead of

char *a;

As you can see here:
http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

Well it doesn't matter at all for compiler, as long as you delare one var per line(as TheIdeasMan explained). Vlad said some harsh words about declaring it like char*, but personally I prefer doing it this way.

Most of the time, I declare only one pointer/reference/etc per line, and it simply shows you instantly what type you are using. In the end, it's char* type. I like it that way. Do as you prefer. Compiler won't mind.
Most of the time, I declare only one pointer/reference/etc per line, and it simply shows you instantly what type you are using. In the end, it's char* type. I like it that way. Do as you prefer. Compiler won't mind.

This.

The char* a, b, c; issue is a weird inconsistency of C that's been inherited by C++. In most contexts in C/C++, char* is treated syntactically as a type in the same way as, say, char would. Writing char* a separates the type from the variable name in a natural way.

Yes, you have to remember the char* a, b, c; gotcha, but, like MatthewRock, I tend to declare one variable per line, so it's never an issue for me.

EDIT: If I really, really wanted to declare multiple variables in a line, and make one of them a pointer, I would make an exception to my usual style and write:

char *a, b, c;

just to make it clear that only a is a pointer, and the rest are chars.

But I can't imagine any circumstances under which I would decide to declare multiple variables of different types on a single line like that.
Last edited on
Topic archived. No new replies allowed.