开发者

C - initialization of pointers, asterisk position [duplicate]

开发者 https://www.devze.com 2023-01-25 06:30 出处:网络
This question already has answers here: Placement of the asterisk in pointer declarations (14 answers) Difference between int* p and int *p declaration [duplicate]
This question already has answers here: Placement of the asterisk in pointer declarations (14 answers) Difference between int* p and int *p declaration [duplicate] (3 answers) difference between int* i and int *i (9 answers) Closed 4 years ago.

What is the most proper way to p开发者_如何学运维lace asterisk? Why?

1)    type*     var;
2)    type      *var;


It does not matter as far as you are declaring only one pointer. It is usually writen like in the second example (in the code I usually read/write) but for the compiler it's the same.

The trouble can come out if you are declaring more than one pointer. For example, this is not declaring two pointer, instead it declares one pointer and one var of type type.

type* var1, var2;

You need to do instead:

type* var1, *var2;

I prefer to use the * by the var always.


Pointer is the type, and I think it makes most sense to group the type information:

int* foo;

This can lead to confusion if several variables are defined on the same line:

int* foo, bar;  // foo will be int*, bar will be int

The solution to this is, never declare several variables on the same line. Something that Code Complete advocates, anyway.


Both work. I'd argue that #1 is clearer in general, but misleading in C and can lead to errors, e.g.:

type* var1, var2;

// This actually means:

type *var1;
type var2;

So I'd say that #2 is more idiomatic in C and therefore recommended, especially if you are not the only programmer working on the code (unless, of course, you all agree on a style).


As others have pointed out, both work fine, and the wrong one actually has the advantage of syntactic economy.

The way I look at it, a declaration consists of type followed by the variable.

<type> <variable>;

int   ii;
int* pii;

So, if you take away the variable, what remains is the type. Which is int and int* above. The compiler treats int* as a type internally (which is pointer to an int).

C, unfortunately, does not support this syntax seamlessly. When declaring multiple variables of the same type, you should be able to do this:

<type> <variable>, <variable>, <variable>;

which you cannot with a pointer type:

int* ii1, ii2, ii3;

declares ii1 of type int* and the rest of type int. To avoid this, I make it a habit of declaring only one variable per line.


I've heard it argued that technically the * is a modifier to the variable, and this is evidenced by the need to use * multiple times in multi variable declarations eg. int *p, *q, *r;

However I like to think of it as a modifier to the base type because that is what appears in prototypes. eg. void func(int*);

.PMCD.

PS. I know I haven't helped your problem :)


The second mode is correct. The other mode are not so clear for a novice programmer. The form int* variable is usually discouraged


That would rather be a coder's preference.

For me, I declare pointers by:

int * var1, var2;

Wherein var1 is a pointer, and var2 is not. If you want to declare multiple pointers in a line:

int * var1, * var2;

And of course, using the other ways are valid.


Declaration semantics follow expression semantics (specifically operator precedence) and I find more complex declarations easier to read using the second style.

You can think of

int *foo;

declaring the type of the expression *foo (ie indirection operator applied to foo) to be int instead of foo being declared to be of type int *.

Whatever convention you choose, just try to be consistent.


There is no single "most proper place".

Usually it is the place the rest of the code uses.

If you're writing your own code from the beginning and can choose your own convention: choose your own convention and stick with it.

If you choose something other than type * var you will meet 'awkward' situations:

/* write these with another style */
int * var;
int const * var;
int * restrict var;

PS. It shouldn't matter, but I usually use type *var;

0

精彩评论

暂无评论...
验证码 换一张
取 消