Kvalifikācija konst var lietot jebkura mainīgā deklarācijai, lai norādītu, ka tā vērtība netiks mainīta (kas ir atkarīgs no tā, kur tiek glabāti const mainīgie, mēs varam mainīt mainīgā const vērtību, izmantojot rādītāju). Rezultāts ir ieviešanas definēts, ja tiek mēģināts mainīt konst.
Apzīmētāja const izmantošana valodā C ir laba prakse, ja vēlamies nodrošināt, lai dažas vērtības paliktu nemainīgas un tās netiktu nejauši mainītas.
avl koki
C programmēšanā const kvalifikatoru var izmantot dažādos kontekstos, lai nodrošinātu dažādas darbības. Šeit ir daži dažādi const kvalifikatora lietošanas gadījumi C:
1. Pastāvīgie mainīgie
const int var = 100;>
Šajā gadījumā mainīgā deklarēšanai izmanto const bija kā konstante ar sākotnējo vērtību 100. Šī mainīgā vērtību nevar mainīt, kad tas ir inicializēts. Skatiet šādu piemēru:
C
// 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;> }> |
>
>
Izvade
./Solution.cpp: In function 'int main()': ./Solution.cpp:11:9: error: assignment of read-only variable 'var' var = 200; ^>
2. Norādiet uz Constant
const int* ptr;>
VAI
int const *ptr;>
Mēs varam mainīt rādītāju, lai norādītu uz jebkuru citu veselu mainīgo, bet nevar mainīt objekta (entītijas) vērtību, kas norādīta, izmantojot rādītāju ptr. Rādītājs tiek saglabāts lasīšanas un rakstīšanas apgabalā (šajā gadījumā steka). Norādītais objekts var būt tikai lasīšanas vai lasīšanas un rakstīšanas apgabalā. Apskatīsim šādus piemērus.
1. piemērs:
C
ko nozīmē google
// 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;> }> |
>
>
Izvade
./Solution.c: In function 'main': ./Solution.c:12:10: error: assignment of read-only location '*ptr' *ptr = 100; ^>
2. piemērs. Programma, kurā mainīgais i pats par sevi ir nemainīgs.
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;> }> |
>
>
Izvade
./Solution.c: In function 'main': ./Solution.c:18:10: error: assignment of read-only location '*ptr' *ptr = 100; ^>
Kvalifikācija zemāka nav atļauts C++ un var izraisīt brīdinājumus C. Down kvalifikācija attiecas uz situāciju, kad kvalificēts tips tiek piešķirts nekvalificētam tipam.
3. piemērs. Programma kvalifikācijas norādīšanai.
in.next java
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;> }> |
>
>
java apakšvirkne satur
Izvade
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. Pastāvīgs rādītājs uz mainīgo
int* const ptr;>
Iepriekš minētā deklarācija ir nemainīgs rādītājs uz vesela skaitļa mainīgo, kas nozīmē, ka mēs varam mainīt tā objekta vērtību, uz kuru norāda rādītājs, bet nevar mainīt rādītāju, lai norādītu uz citu mainīgo.
Piemērs
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;> }> |
>
>
indiešu aktrise Rani Mukerji
Izvade
./Solution.c: In function 'main': ./Solution.c:15:9: error: assignment of read-only variable 'ptr' ptr = &j; /* error */ ^>
4. Constant Pointer to Constant
const int* const ptr;>
Iepriekš minētā deklarācija ir konstants rādītājs uz konstantu mainīgo, kas nozīmē, ka mēs nevaram mainīt rādītāja norādīto vērtību, kā arī mēs nevaram norādīt rādītāju uz citiem mainīgajiem. Apskatīsim ar piemēru.
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;> }> |
>
>
Izvade
./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 */ ^>
Const kvalifikāciju priekšrocības C
Const kvalifikācijai C ir šādas priekšrocības:
- Uzlabota koda lasāmība: atzīmējot mainīgo kā const, jūs norādāt citiem programmētājiem, ka tā vērtību nevajadzētu mainīt, padarot kodu vieglāk saprotamu un uzturējamu. Uzlabota tipa drošība: izmantojot const, varat nodrošināt, ka vērtības netiek nejauši mainītas, samazinot kļūdu un kļūdu iespējamību kodā. Uzlabota optimizācija: kompilatori var efektīvāk optimizēt const mainīgos, jo viņi zina, ka programmas izpildes laikā to vērtības nemainīsies. Tas var radīt ātrāku un efektīvāku kodu. Labāks atmiņas lietojums: deklarējot mainīgos kā const, bieži vien varat izvairīties no to vērtību kopēšanas, kas var samazināt atmiņas lietojumu un uzlabot veiktspēju. Uzlabota saderība : deklarējot mainīgos kā const, varat padarīt savu kodu saderīgāku ar citām bibliotēkām un API, kas izmanto mainīgos const. Uzlabota uzticamība : izmantojot const, varat padarīt savu kodu uzticamāku, jo varat nodrošināt, ka vērtības netiek negaidīti mainītas, samazinot kļūdu un kļūdu risku kodā.
Kopsavilkums
Tips | Deklarācija | Rādītāja vērtības maiņa (*ptr = 100) | Norādes vērtības maiņa (ptr = &a) |
---|---|---|---|
Rādītājs uz mainīgo | int * ptr | Jā | Jā |
Norādiet uz Constant | const int * ptr int const * ptr | Nē | Jā |
Pastāvīgs rādītājs uz mainīgo | int * const ptr | Jā | Nē |
Pastāvīgais rādītājs uz konstanti | const int * const ptr | Nē | Nē |
Šo rakstu ir apkopojis Narendra Kangralkar .