日韩在线免费播放-日韩在线免费av-日韩在线免费-日韩在线毛片-国产高清不卡视频-国产高清不卡

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

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

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

它的規則: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

現在我們對變量進行下修改

  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.常量化指針目標表達式,限制通過指針改變其目標的數值。一般形式如下:

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

請看下面示例代碼:

  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

現在我們對指針的目標進行修改:

  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來改變目標地址的內容,最常見的用法就是main函數的參數。

2.常量化指針變量

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

<數據類型> * 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

現在我們來修改下代碼:

  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 <數據類型> * 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

現在我們來修改下代碼:

  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;

     ^

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

上一篇:C語言scanf函數用法

下一篇:什么是結構體

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

回到頂部

主站蜘蛛池模板: 密会韩剧| 蒋芸| 陈慧娴个人资料| cctv16体育节目表今天目表| 乐之路| 电影《武状元苏乞儿》| 夜店 电影| 捆绑上天堂电影| 疯狂试爱二| 性感的秘书| 视频偷窥| 女朋友的舅妈| 少年歌行第三季| 我的电影在线观看| 幻乐森林演员表| 红色诗歌配画| 家族游戏| 一元二次方程实际问题| 色女在线| 遥远星际| 东方电视台节目表今日节目| 单身情歌 歌词| 俺去也电影网| 凌靖| 真实游戏在线观看免费完整版| 非法制裁| 韩宇辰| 一年又一年电视剧演员表| 公民的基本权利和义务教学设计| www.56.com| 绝对权力在线观看免费| 拔萝卜电影| 在线观看www视频| 隐藏的歌手中国版全集| 延边卫视节目表| 狂魔电影| 即日启程 电影| 电影白夜行| 张鸿昌| 台版缉魂2小时10分版网飞版| 成年奶妈|