C语言const 关键字
面試的時候,應該有遇到const相關的,畢竟也是學習中的一個知識點,看完我們這篇文章,我覺得你應該可以在面試中完完全全的吃透const這個點。
const和變量
const uint32_t hello = 3;編譯的時候,編譯器就知道了 hello 這個變量是不可以被修改了,const其實也就是read only,你只能讀我的,不能修改我。
所以你要是試圖修改這個變量的值,編譯器會告訴你
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有一個特點,只要const 在變量名之前就可以,所以 const uint32_t i; 和uint32_t const i都是一樣的。
const uint32_t hello = 3;uint32_t const hello = 3;const 和函數參數
我們有時候不希望函數體內的代碼修改呢我們傳入的參數,我們就會這樣做。我們看看下面這個代碼
#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); }
我們聲明的時候,沒有加上const ,但是我們定義函數的時候,我們加上了const,編譯沒有報錯,也成功執行了。聲明只是一個虛張聲勢而已,就好比嚴令禁止一樣,有些人還是繼續作惡,就好比。
我解釋得再深入一些,我們知道函數形參(非指針或者數組)傳過來的是一個拷貝,函數體里面處理的是這個形參的拷貝而不是這個形參的實體,就像影分身一樣,分身死了,本體還在著呢。所以對分身的屬性和本體的屬性是不一樣的。
#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 和數組
#include "stdio.h"#include "stdint.h"
const uint16_t things[] = {5, 6, 7, 8, 9};
int main(){ things[1] = 12; return (1); }
編譯一下,就會報錯
8 12 G:\c\1.cpp [Error] assignment of read-only location 'things[1]'const 和結構體
例子
#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); }
編譯的時候
13 16 G:\c\1.cpp [Error] assignment of member 'aStruct::a' in read-only object我們也可以在結構體里面指明const,這樣就不會影響整個結構體,而只影響里面的某一個參數了。
const 和指針
const 修飾指針變量指向的內容
指針和const應該是最常見了,畢竟指針一邊,就好比火車脫軌了,開往了錯誤的方向。
#include "stdio.h"#include "stdint.h"uint64_t bob = 42;uint64_t const *aFour = &bob;
int main(){ *aFour = 4; return (1); }
一樣的,編譯的時候,還是會出錯
9 9 G:\c\1.cpp [Error] assignment of read-only location '* aFour'const 修飾的是指針指向的內容,說明指針執行的內容是read only的,是不可改變。
所以,下面這個代碼是沒有問題的
#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 修飾指針變量,那就說明這個地址是不可變的。
#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'
所以如果我們希望一個指針變量不能改變,指向的內容也不能改變,就應該這樣寫
uint32_t bob = 32;uint32_t const *const p = &bob;const 和#define的區別
既然有了#define 還需要const 嗎?他們的恩恩怨怨我覺得今天應該要結束了,即使沒有結束,也應該分出一個勝負了。
?#define 是宏替換,在預編譯的時候確定?const 前面有數據類型,會進行數據類型檢查?#define 執行的宏名,可以通過#undef來取消宏定義,但是const修飾的變量,保存在靜態區,會一直存在,所以如果你有多個頭文件聲明同一個const 變量,肯定會出現重復定義。但是你在兩個頭文件中,#define 兩個名字一樣的宏名,并不會有問題
如果看了上面還不過癮,推薦一個const的文章,因為沒有獲得作轉載許可,只給出鏈接
C語言之 const 不變量
掃碼或長按關注
回復「?加群?」進入技術群聊
總結
以上是生活随笔為你收集整理的C语言const 关键字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大家都说 Java 反射效率低,你知道原
- 下一篇: 苹果678外观有什么区别(苹果官网报价)