国产成人精品三级麻豆,色综合天天综合高清网,亚洲精品夜夜夜,国产成人综合在线女婷五月99播放,色婷婷色综合激情国产日韩

當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 > const的作用

const的作用 時間:2019-05-24      來源:華清遠見

在c語言中,關鍵字const修飾變量,可以使得變量常量化。所謂的常量化,就意味著“readonly”。

它的規(guī)則:const離誰近,誰就不能被修改

下面我們一起來通過示例來看下:

一、const修飾變量

  1 #include <stdio.h>                                                       

  2                                      

  3 int main(int argc, const char *argv[])

  4 {

  5     const int a = 10;

  6     int const b = 10;

  7 

  8     printf("a = %d,b = %d\n",a,b);                                          

  9     return 0;

 10 }

程序運行結果如下:

yxl@ubuntu:~$ gcc test.c 

yxl@ubuntu:~$ ./a.out 

a = 10,b = 10

現(xiàn)在我們對變量進行下修改

  1 #include <stdio.h>

  2 

  3 int main(int argc, const char *argv[])

  4 {

  5     const int a = 10;

  6     int const b = 10;

  7 

  8     a = 20;

  9     b = 30;                                                                 

 10 

 11     printf("a = %d,b = %d\n",a,b);

 12     return 0;

 13 }

程序運行結果如下:

yxl@ubuntu:~$ gcc test.c 

test.c: In function ‘main’:

test.c:8:4: error: assignment of read-only variable ‘a’

  a = 20;

    ^

test.c:9:4: error: assignment of read-only variable ‘b’

  b = 30;

^

上面兩種寫法都是允許的,變量a和b有const修飾,則a和b的值不能被修改。

二、const修飾指針

1.常量化指針目標表達式,限制通過指針改變其目標的數(shù)值。一般形式如下:

const  <數(shù)據(jù)類型> *<指針變量名> [=<指針運算表達式>] 

請看下面示例代碼:

  1 #include <stdio.h>

  2 

  3 int main(int argc, const char *argv[])

  4 {

  5     int a = 10,b = 20;

  6     const int *p;

  7 

  8     p = &a;

  9     printf("a = %d,*p = %d\n",a,*p);

 10 

 11     p = &b;                                                                 

 12     printf("b = %d,*p = %d\n",b,*p);

 13     return 0;

 14 }

程序運行結果如下:

yxl@ubuntu:~$ gcc test.c 

yxl@ubuntu:~$ ./a.out 

a = 10,*p = 10

b = 20,*p = 20

現(xiàn)在我們對指針的目標進行修改:

  1 #include <stdio.h>

  2 

  3 int main(int argc, const char *argv[])

  4 {

  5     int a = 10,b = 20;

  6     const int *p;

  7 

  8     p = &a;

  9     printf("a = %d,*p = %d\n",a,*p);

 10 

 11     p = &b;

 12     printf("b = %d,*p = %d\n",b,*p);

 13 

 14     *p = 30;

 15     printf("*p = %d\n",*p);                                                 

 16 

 17     return 0;

 18 }

程序運行結果如下:

yxl@ubuntu:~$ gcc test.c 

test.c: In function ‘main’:

test.c:14:5: error: assignment of read-only location ‘*p’

  *p = 30;

     ^

通過以上測試,我們沒有辦法通過p來改變目標地址的內(nèi)容,最常見的用法就是main函數(shù)的參數(shù)。

2.常量化指針變量

常量化指針變量,是的指針變量存儲的地址值不能修改,一般說明形式如下:

<數(shù)據(jù)類型> * const  <指針變量名> [=<指針運算表達式>] 

請看下面示例代碼:

  1 #include <stdio.h>

  2 

  3 int main(int argc, const char *argv[])

  4 {

  5     int a = 10;                                                             

  6     int * const p = &a;

  7 

  8     printf("a = %d,*p = %d\n",a,*p);

  9     return 0;

 10 }

程序運行結果如下:

yxl@ubuntu:~$ gcc test.c 

yxl@ubuntu:~$ ./a.out 

a = 10,*p = 10

現(xiàn)在我們來修改下代碼:

  1 #include <stdio.h>

  2 

  3 int main(int argc, const char *argv[])

  4 {

  5     int a = 10,b = 20;

  6     int * const p = &a;

  7     p = &b;                                                                 

  8 

  9     printf("a = %d,*p = %d\n",a,*p);

 10     return 0;

 11 }

程序運行結果如下:

yxl@ubuntu:~$ gcc test.c 

test.c: In function ‘main’:

test.c:7:4: error: assignment of read-only variable ‘p’

  p = &b;

^    

通過以上示例,我們可以看出此時指針變量的指向是不可以修改的。

3.常量化指針變量及其目標表達式,一般說明形式如下:

const <數(shù)據(jù)類型> * const  <指針變量名> [=<指針運算表達式>] 

請看下面示例代碼:

  1 #include <stdio.h>

  2 

  3 int main(int argc, const char *argv[])

  4 {

  5     int a = 10;                                                             

  6     const int * const p = &a;

  7 

  8     printf("a = %d,*p = %d\n",a,*p);

  9     return 0;

 10 }

程序運行結果如下:

yxl@ubuntu:~$ gcc test.c 

.yxl@ubuntu:~$ ./a.out 

a = 10,*p = 10

現(xiàn)在我們來修改下代碼:

  1 #include <stdio.h>

  2 

  3 int main(int argc, const char *argv[])

  4 {

  5     int a = 10,b = 20;

  6     const int * const p = &a;

  7 

  8     p = &b;

  9     *p = 30;

 10                                                                             

 11     printf("a = %d,*p = %d\n",a,*p);

 12     return 0;

 13 }

程序運行結果如下:

yxl@ubuntu:~$ gcc test.c 

test.c: In function ‘main’:

test.c:8:4: error: assignment of read-only variable ‘p’

  p = &b;

    ^

test.c:9:5: error: assignment of read-only location ‘*p’

  *p = 30;

     ^

通過以上示例,我們可以看出,此時指針的指向和目標的內(nèi)容都不可以修改。

上一篇:C語言scanf函數(shù)用法

下一篇:什么是結構體

熱點文章推薦
華清學員就業(yè)榜單
高薪學員經(jīng)驗分享
熱點新聞推薦
前臺專線:010-82525158 企業(yè)培訓洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠見科技集團有限公司 版權所有 ,京ICP備16055225號-5,京公海網(wǎng)安備11010802025203號

回到頂部