C语言const 关键字
面試的時(shí)候,應(yīng)該有遇到const相關(guān)的,畢竟也是學(xué)習(xí)中的一個(gè)知識(shí)點(diǎn),看完我們這篇文章,我覺得你應(yīng)該可以在面試中完完全全的吃透const這個(gè)點(diǎn)。
const和變量
const uint32_t hello = 3;編譯的時(shí)候,編譯器就知道了 hello 這個(gè)變量是不可以被修改了,const其實(shí)也就是read only,你只能讀我的,不能修改我。
所以你要是試圖修改這個(gè)變量的值,編譯器會(huì)告訴你
clang-700.1.81:error: read-only variable is not assignable hello++; ~~~~~^error: read-only variable is not assignable hello = 92; ~~~~~ ^gcc-5.3.0:error: increment of read-only variable 'hello' hello++; ^error: assignment of read-only variable 'hello' hello = 92; ^
C有一個(gè)特點(diǎn),只要const 在變量名之前就可以,所以 const uint32_t i; 和uint32_t const i都是一樣的。
const uint32_t hello = 3;uint32_t const hello = 3;const 和函數(shù)參數(shù)
我們有時(shí)候不希望函數(shù)體內(nèi)的代碼修改呢我們傳入的參數(shù),我們就會(huì)這樣做。我們看看下面這個(gè)代碼
#include "stdio.h"#include "stdint.h"void printTwo(uint32_t a, uint64_t b);
void printTwo(const uint32_t a, const uint64_t b) { printf("%d %d\n", a, b);}
int main(){ printTwo(12,23); return (1); }
我們聲明的時(shí)候,沒有加上const ,但是我們定義函數(shù)的時(shí)候,我們加上了const,編譯沒有報(bào)錯(cuò),也成功執(zhí)行了。聲明只是一個(gè)虛張聲勢(shì)而已,就好比嚴(yán)令禁止一樣,有些人還是繼續(xù)作惡,就好比。
我解釋得再深入一些,我們知道函數(shù)形參(非指針或者數(shù)組)傳過來的是一個(gè)拷貝,函數(shù)體里面處理的是這個(gè)形參的拷貝而不是這個(gè)形參的實(shí)體,就像影分身一樣,分身死了,本體還在著呢。所以對(duì)分身的屬性和本體的屬性是不一樣的。
#include "stdio.h"#include "stdint.h"void printTwo(const uint32_t a, const uint64_t b);
void printTwo(const uint32_t a, uint64_t b) { b = 12; printf("%d %d\n", a, b);}
int main(){ printTwo(12,23); return (1); }
const 和數(shù)組
#include "stdio.h"#include "stdint.h"
const uint16_t things[] = {5, 6, 7, 8, 9};
int main(){ things[1] = 12; return (1); }
編譯一下,就會(huì)報(bào)錯(cuò)
8 12 G:\c\1.cpp [Error] assignment of read-only location 'things[1]'const 和結(jié)構(gòu)體
例子
#include "stdio.h"#include "stdint.h"struct aStruct { int32_t a; uint64_t b;};
const struct aStruct someStructA = {.a = 3, .b = 4};
int main(){ someStructA.a = 12; return (1); }
編譯的時(shí)候
13 16 G:\c\1.cpp [Error] assignment of member 'aStruct::a' in read-only object我們也可以在結(jié)構(gòu)體里面指明const,這樣就不會(huì)影響整個(gè)結(jié)構(gòu)體,而只影響里面的某一個(gè)參數(shù)了。
const 和指針
const 修飾指針變量指向的內(nèi)容
指針和const應(yīng)該是最常見了,畢竟指針一邊,就好比火車脫軌了,開往了錯(cuò)誤的方向。
#include "stdio.h"#include "stdint.h"uint64_t bob = 42;uint64_t const *aFour = &bob;
int main(){ *aFour = 4; return (1); }
一樣的,編譯的時(shí)候,還是會(huì)出錯(cuò)
9 9 G:\c\1.cpp [Error] assignment of read-only location '* aFour'const 修飾的是指針指向的內(nèi)容,說明指針執(zhí)行的內(nèi)容是read only的,是不可改變。
所以,下面這個(gè)代碼是沒有問題的
#include "stdio.h"#include "stdint.h"uint64_t bob = 42;uint64_t const *aFour = &bob;uint64_t b = 32;int main(){ aFour = &b; return (1); }
const 修飾指針變量
同理如果const 修飾指針變量,那就說明這個(gè)地址是不可變的。
#include "stdio.h"#include "stdint.h"uint64_t bob = 42;uint64_t *const aFour = &bob;uint64_t b = 32;int main(){ aFour = &b; return (1); }9 8 G:\c\1.cpp [Error] assignment of read-only variable 'aFour'
所以如果我們希望一個(gè)指針變量不能改變,指向的內(nèi)容也不能改變,就應(yīng)該這樣寫
uint32_t bob = 32;uint32_t const *const p = &bob;const 和#define的區(qū)別
既然有了#define 還需要const 嗎?他們的恩恩怨怨我覺得今天應(yīng)該要結(jié)束了,即使沒有結(jié)束,也應(yīng)該分出一個(gè)勝負(fù)了。
?#define 是宏替換,在預(yù)編譯的時(shí)候確定?const 前面有數(shù)據(jù)類型,會(huì)進(jìn)行數(shù)據(jù)類型檢查?#define 執(zhí)行的宏名,可以通過#undef來取消宏定義,但是const修飾的變量,保存在靜態(tài)區(qū),會(huì)一直存在,所以如果你有多個(gè)頭文件聲明同一個(gè)const 變量,肯定會(huì)出現(xiàn)重復(fù)定義。但是你在兩個(gè)頭文件中,#define 兩個(gè)名字一樣的宏名,并不會(huì)有問題
如果看了上面還不過癮,推薦一個(gè)const的文章,因?yàn)闆]有獲得作轉(zhuǎn)載許可,只給出鏈接
C語言之 const 不變量
掃碼或長(zhǎng)按關(guān)注
回復(fù)「?加群?」進(jìn)入技術(shù)群聊
總結(jié)
以上是生活随笔為你收集整理的C语言const 关键字的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大家都说 Java 反射效率低,你知道原
- 下一篇: 五分钟搞定正则表达式,如果没搞定,再加两