當(dāng)前位置:首頁(yè) > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > const的作用
const的作用
時(shí)間:2019-05-24 來(lái)源:華清遠(yuǎn)見(jiàn)
在c語(yǔ)言中,關(guān)鍵字const修飾變量,可以使得變量常量化。所謂的常量化,就意味著“readonly”。
它的規(guī)則:const離誰(shuí)近,誰(shuí)就不能被修改
下面我們一起來(lái)通過(guò)示例來(lái)看下:
一、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 }
程序運(yùn)行結(jié)果如下:
yxl@ubuntu:~$ gcc test.c
yxl@ubuntu:~$ ./a.out
a = 10,b = 10
現(xiàn)在我們對(duì)變量進(jì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 }
程序運(yùn)行結(jié)果如下:
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;
^
上面兩種寫(xiě)法都是允許的,變量a和b有const修飾,則a和b的值不能被修改。
二、const修飾指針
1.常量化指針目標(biāo)表達(dá)式,限制通過(guò)指針改變其目標(biāo)的數(shù)值。一般形式如下:
const <數(shù)據(jù)類型> *<指針變量名> [=<指針運(yùn)算表達(dá)式>]
請(qǐng)看下面示例代碼:
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 }
程序運(yùn)行結(jié)果如下:
yxl@ubuntu:~$ gcc test.c
yxl@ubuntu:~$ ./a.out
a = 10,*p = 10
b = 20,*p = 20
現(xiàn)在我們對(duì)指針的目標(biāo)進(jì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 }
程序運(yùn)行結(jié)果如下:
yxl@ubuntu:~$ gcc test.c
test.c: In function ‘main’:
test.c:14:5: error: assignment of read-only location ‘*p’
*p = 30;
^
通過(guò)以上測(cè)試,我們沒(méi)有辦法通過(guò)p來(lái)改變目標(biāo)地址的內(nèi)容,最常見(jiàn)的用法就是main函數(shù)的參數(shù)。
2.常量化指針變量
常量化指針變量,是的指針變量存儲(chǔ)的地址值不能修改,一般說(shuō)明形式如下:
<數(shù)據(jù)類型> * const <指針變量名> [=<指針運(yùn)算表達(dá)式>]
請(qǐng)看下面示例代碼:
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 }
程序運(yùn)行結(jié)果如下:
yxl@ubuntu:~$ gcc test.c
yxl@ubuntu:~$ ./a.out
a = 10,*p = 10
現(xiàn)在我們來(lái)修改下代碼:
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 }
程序運(yùn)行結(jié)果如下:
yxl@ubuntu:~$ gcc test.c
test.c: In function ‘main’:
test.c:7:4: error: assignment of read-only variable ‘p’
p = &b;
^
通過(guò)以上示例,我們可以看出此時(shí)指針變量的指向是不可以修改的。
3.常量化指針變量及其目標(biāo)表達(dá)式,一般說(shuō)明形式如下:
const <數(shù)據(jù)類型> * const <指針變量名> [=<指針運(yùn)算表達(dá)式>]
請(qǐng)看下面示例代碼:
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 }
程序運(yùn)行結(jié)果如下:
yxl@ubuntu:~$ gcc test.c
.yxl@ubuntu:~$ ./a.out
a = 10,*p = 10
現(xiàn)在我們來(lái)修改下代碼:
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 }
程序運(yùn)行結(jié)果如下:
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;
^
通過(guò)以上示例,我們可以看出,此時(shí)指針的指向和目標(biāo)的內(nèi)容都不可以修改。
華清遠(yuǎn)見(jiàn)90+項(xiàng)目獲批!教育部2021最新協(xié)同育人項(xiàng)目名
華清遠(yuǎn)見(jiàn)榮獲2021騰訊教育“年度口碑影響力職業(yè)教育品
華清遠(yuǎn)見(jiàn)受邀參加2021年武漢民辦高校信息學(xué)科合作聯(lián)盟
溫暖同行共創(chuàng)佳績(jī) 2019華清遠(yuǎn)見(jiàn)北京總部年會(huì)大曝光
助力高校AI人工智能學(xué)科建設(shè) 華清遠(yuǎn)見(jiàn)人工智能師資班
華清遠(yuǎn)見(jiàn)受邀參加四川省物聯(lián)網(wǎng)年會(huì),榮獲優(yōu)秀企業(yè)專家
