Char * foo vs char * foo

A simple question: why are you writing

char *foo;

but not

char* foo;

Let me clarify: for me (coming from Java) the ad looks like

<variable-type> <variable-name>;

In the above case, I declare a variable with a footype name char*(since this is a pointer pointing to a char). But wherever I read c / C ++ / C # -Code, it looks like a variable with a *footype name char. The compiler is not interested in spaces, but I as a developer.

tl; dr What I ask is a good explanation for writing char *fooinstead char* foo(which, as explained, seems more convenient to me).

+5
source share
4 answers

Think of the following announcement:

char *p, c;

char p c. , , .

+11

, , C ++ , " ".

char *foo :

char* foo, bar;

, , , char. char (foo) char (bar).

char, :

char* foo, * bar;

, :

const *foo, *bar;

, * , .

, , , , char* foo , . , .;)

+3

char* var. - . , .

+1

: , . , :

char *a, b, c[5], *d[5];

" char", b "char", c " 5 char" d " 5 char". C ++ , .

However, it refers to type and (type) (name), which is easier to understand for people, so you often see char *. In my opinion, both paths are perfectly fine if you specify only one variable per line when using char *. (This is what many coding standards require anyway.)

+1
source

All Articles