LoginSignup
61
54

More than 5 years have passed since last update.

変数の const 付け方一覧

Posted at

変数への const の付け方をまとめてみました。
ややこしい解説は一切なしです!

値の書き換え禁止

const int a  = 0;
int const a2 = 0;    // 意味は同じ

a = 1;      // コンパイルエラー

ポインタの指す値の書き換え禁止

int b = 0;

const int * p_b  = &b;
int const * p_b2 = &b;    // 意味は同じ

p_b = &b;           // OK
(*p_b) = b;         // コンパイルエラー

ポインタの書き換え禁止

int c = 0;
int * const p_c = NULL;

p_c = &c;       // コンパイルエラー
(*p_c) = c;     // OK

ポインタ、ポインタの指す値の両方の書き換え禁止

int d = 0;
const int * const p_d = NULL;

p_d = &d;       // コンパイルエラー
(*p_d) = &d;    // コンパイルエラー
61
54
0

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
61
54