予選 定数 を任意の変数の宣言に適用して、その値が変更されないことを指定できます (const 変数が格納されている場所に応じて、ポインターを使用して const 変数の値を変更する場合があります)。 const を変更しようとした場合の結果は実装定義です。
C で const 修飾子を使用することは、一部の値を定数のままにし、誤って変更しないようにしたい場合に良い方法です。
C プログラミングでは、const 修飾子をさまざまなコンテキストで使用して、さまざまな動作を提供できます。 C での const 修飾子のさまざまな使用例をいくつか示します。
1. 定数変数
const int var = 100;>
この場合、const を使用して変数を宣言します。 だった 初期値 100 の定数として指定します。この変数の値は、初期化されると変更できません。次の例を参照してください。
C
2か月ぶり
// C program to demonstrate that constant variables can not> // be modified> #include> int> main()> {> > const> int> var = 100;> > // Compilation error: assignment of read-only variable> > // 'var'> > var = 200;> > return> 0;> }> |
>
>
出力
./Solution.cpp: In function 'int main()': ./Solution.cpp:11:9: error: assignment of read-only variable 'var' var = 200; ^>
2. 定数へのポインタ
const int* ptr;>
または
int const *ptr;>
他の整変数を指すようにポインターを変更することはできますが、ポインター ptr を使用して指すオブジェクト (エンティティ) の値を変更することはできません。ポインタは読み書き領域 (この場合はスタック) に格納されます。指定されたオブジェクトは読み取り専用領域または読み取り/書き込み領域にある可能性があります。次の例を見てみましょう。
例 1:
C
// C program to demonstrate that the pointer to point to> // any other integer variable, but the value of the object> // (entity) pointed can not be changed> #include> int> main(> void> )> {> > int> i = 10;> > int> j = 20;> > /* ptr is pointer to constant */> > const> int> * ptr = &i;> > printf> (> 'ptr: %d
'> , *ptr);> > /* error: object pointed cannot be modified> > using the pointer ptr */> > *ptr = 100;> > ptr = &j;> /* valid */> > printf> (> 'ptr: %d
'> , *ptr);> > return> 0;> }> |
>
>
出力
./Solution.c: In function 'main': ./Solution.c:12:10: error: assignment of read-only location '*ptr' *ptr = 100; ^>
例 2: 変数 i 自体が定数であるプログラム。
C
// C program to demonstrate that the pointer to point to> // any other integer variable, but the value of the object> // (entity) pointed can not be changed> #include> int> main(> void> )> {> > /* i is stored in read only area*/> > int> const> i = 10;> > int> j = 20;> > /* pointer to integer constant. Here i> > is of type 'const int', and &i is of> > type 'const int *'. And p is of type> > 'const int', types are matching no issue */> > int> const> * ptr = &i;> > printf> (> 'ptr: %d
'> , *ptr);> > /* error */> > *ptr = 100;> > /* valid. We call it up qualification. In> > C/C++, the type of 'int *' is allowed to up> > qualify to the type 'const int *'. The type of> > &j is 'int *' and is implicitly up qualified by> > the compiler to 'const int *' */> > ptr = &j;> > printf> (> 'ptr: %d
'> , *ptr);> > return> 0;> }> |
>
Javaの配列リストを並べ替える
>
出力
Javaの配列のオブジェクト
./Solution.c: In function 'main': ./Solution.c:18:10: error: assignment of read-only location '*ptr' *ptr = 100; ^>
ダウン資格 は C++ では許可されておらず、C では警告が発生する可能性があります。下位修飾とは、修飾された型が修飾されていない型に割り当てられる状況を指します。
例 3: 資格を証明するプログラム。
C
// C program to demonstrate the down qualification> #include> int> main(> void> )> {> > int> i = 10;> > int> const> j = 20;> > /* ptr is pointing an integer object */> > int> * ptr = &i;> > printf> (> '*ptr: %d
'> , *ptr);> > /* The below assignment is invalid in C++, results in> > error In C, the compiler *may* throw a warning, but> > casting is implicitly allowed */> > ptr = &j;> > /* In C++, it is called 'down qualification'. The type> > of expression &j is 'const int *' and the type of ptr> > is 'int *'. The assignment 'ptr = &j' causes to> > implicitly remove const-ness from the expression &j.> > C++ being more type restrictive, will not allow> > implicit down qualification. However, C++ allows> > implicit up qualification. The reason being, const> > qualified identifiers are bound to be placed in> > read-only memory (but not always). If C++ allows> > above kind of assignment (ptr = &j), we can use 'ptr'> > to modify value of j which is in read-only memory.> > The consequences are implementation dependent, the> > program may fail> > at runtime. So strict type checking helps clean code.> > */> > printf> (> '*ptr: %d
'> , *ptr);> > return> 0;> }> |
>
>
出力
main.c: In function ‘main’: main.c:16:9: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers] 16 | ptr = &j; | ^ *ptr: 10 *ptr: 20>
3. 変数への定数ポインタ
int* const ptr;>
上記の宣言は整変数への定数ポインタです。つまり、ポインタが指すオブジェクトの値は変更できますが、ポインタを別の変数を指すように変更することはできません。
例
C
// C program to demonstrate that the value of object pointed> // by pointer can be changed but the pointer can not point> // to another variable> #include> int> main(> void> )> {> > int> i = 10;> > int> j = 20;> > /* constant pointer to integer */> > int> *> const> ptr = &i;> > printf> (> 'ptr: %d
'> , *ptr);> > *ptr = 100;> /* valid */> > printf> (> 'ptr: %d
'> , *ptr);> > ptr = &j;> /* error */> > return> 0;> }> |
>
JSのグローバル変数
>
出力
./Solution.c: In function 'main': ./Solution.c:15:9: error: assignment of read-only variable 'ptr' ptr = &j; /* error */ ^>
4. 定数への定数ポインタ
const int* const ptr;>
上記の宣言は定数変数への定数ポインタです。これは、ポインタが指す値を変更できないこと、およびポインタが他の変数を指すことができないことを意味します。例を挙げて見てみましょう。
C
// C program to demonstrate that value pointed by the> // pointer can not be changed as well as we cannot point the> // pointer to other variables> #include> int> main(> void> )> {> > int> i = 10;> > int> j = 20;> > /* constant pointer to constant integer */> > const> int> *> const> ptr = &i;> > printf> (> 'ptr: %d
'> , *ptr);> > ptr = &j;> /* error */> > *ptr = 100;> /* error */> > return> 0;> }> |
>
>
出力
./Solution.c: In function 'main': ./Solution.c:12:9: error: assignment of read-only variable 'ptr' ptr = &j; /* error */ ^ ./Solution.c:13:10: error: assignment of read-only location '*ptr' *ptr = 100; /* error */ ^>
C における const 修飾子の利点
C の const 修飾子には次の利点があります。
- コードの可読性の向上: 変数を const としてマークすると、その値を変更すべきでないことを他のプログラマに示し、コードの理解と保守が容易になります。強化された型安全性 : const を使用すると、値が誤って変更されることがなくなり、コード内のバグやエラーが発生する可能性が減ります。最適化の改善: コンパイラーは、プログラムの実行中に const 変数の値が変更されないことを認識しているため、const 変数をより効果的に最適化できます。これにより、コードがより高速かつ効率的になる可能性があります。メモリ使用量の向上: 変数を const として宣言すると、多くの場合、変数の値のコピーを作成する必要がなくなり、メモリ使用量が削減され、パフォーマンスが向上します。互換性の向上 : 変数を const として宣言することで、const 変数を使用する他のライブラリや API とコードの互換性を高めることができます。信頼性の向上 : const を使用すると、値が予期せず変更されることがなくなり、コード内のバグやエラーのリスクが軽減されるため、コードの信頼性が向上します。
まとめ
タイプ | 宣言 | ポインタ値の変更 (*ptr = 100) | ポインティング値の変更 (ptr = &a) |
---|---|---|---|
変数へのポインタ | int * ptr | はい | はい |
定数へのポインタ | const int * ptr int const * ptr | いいえ | はい |
変数への定数ポインタ | int * const ptr | はい | いいえ |
定数への定数ポインタ | const int * const ptr | いいえ | いいえ |
この記事をまとめたのは、 ナレンドラ・カングラルカール 。