42
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語でのポインタ変数宣言時の *(アスタリスク)の位置

Last updated at Posted at 2015-02-15

Cライクな言語でポインタ変数を宣言する際のアスタリスクの位置でどこの土壌でコーディングしてたかがちょっとわかりますよね。

書き方は大体3種類に分かれると思ってて

int *foo;   // (1)
int* foo;   // (2)
int * foo;  // (3)

です。

まず、結論から言うと人それぞれでいいと思います。バグらなければ。

もちろんコーディングスタイルで決まっているならそれに沿うべきですが、(2)で記述した人が間違った解釈さえしなければどっちでもいい。バグらなければ。

(2) int* foo; の副作用

副作用、ってほど大事ではないのですが、アスタリスクを型にくっつける人はintint*を別として、それぞれが確立された型と認識していることがあります。

そうなるとどうなるか、下記のような勘違いを起こす可能性があります。

int *foo, *bar;  // 両方ともポインタ変数
int* foo, bar;   // fooだけポインタ変数、barは普通のint

これされるとバグの温床になる得る。

int* foo;
int* bar;

こうすればおk。

ちなみに

僕は (1) の int *foo; 派です。

また、Objective-Cの流儀は(1)です。(たしか

もしかしたら

コンパイラによって解釈違うのかもしれないですね。
(というより、昔なにかで見た気がするのですが、勘違いですかね?)

おまけ

42
28
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
42
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?