文章出處

《OOC》筆記(1)——C語言const、static和extern的用法

C語言中const關鍵字用法不少,我只喜歡兩種用法。一是用于修飾函數形參,二是用于修飾全局變量和局部變量。

用const修飾的函數形參

直接修飾

一個形如

int Minus(const int a, const int b, int testCase);

的函數,const的意義是什么呢?

答:參數a被const修飾,說明在Minus函數內,編譯器不允許a被別的變量(例如x)賦值(修改)。參數b同理。

如果你寫了a = x; 這樣的語句,編譯器會警告"warning: assignment of read-only location",即"警告:賦值到只讀位置"。

 1 int Minus(const int a, const int b, int testCase)
 2 {
 3     int x, y;
 4     switch (testCase)
 5     {
 6     case 0:
 7         a = x; //warning: assignment of read-only location
 8         b = y; //warning: assignment of read-only location
 9         break;
10     case 1:
11         a = (const int)x; //warning: assignment of read-only location
12         b = (const int)y; //warning: assignment of read-only location
13         break;
14     case 2:
15         x = a; //OK with compiler.
16         y = b; //OK with compiler.
17         break;
18     case 3:
19         x = (int)a; //OK with compiler.
20         y = (int)b; //OK with compiler.
21         break;
22     default:
23         break;
24     }
25     int result = a - b;
26     return result;
27

指針修飾

請原諒這個不靠譜的的叫法吧。總之,一個形如

int Add(const int * a, const int * b, int testCase);

的函數,const的意義是什么呢?

答:參數a是指向int的指針,a被const修飾,說明在Add函數內,a指向的內容不能被賦值(修改)。如果將a賦值給另一個int*類型的指針x,那么就可以通過x修改a指向的內容。這違反了const的作用。因此,編譯器禁止將a賦值給別的變量。參數b同理。

如果你寫了x = a; 這樣的語句,編譯器會警告"warning: assignment discards qualifiers from pointer target type",即"警告:賦值無視目標指針的修飾符"。

 1 int Add(const int * a, const int * b, int testCase)
 2 {
 3     int * x;
 4     int * y;
 5     switch (testCase)
 6     {
 7     case 0:
 8         a = x; //OK with compiler.
 9         b = y; //OK with compiler.
10         break;
11     case 1:
12         a = (const int *)x; //OK with compiler.
13         b = (const int *)y; //OK with compiler.
14         break;
15     case 2:
16         x = a; //warning: assignment discards qualifiers from pointer target type
17         y = b; //warning: assignment discards qualifiers from pointer target type
18         break;
19     case 3:
20         x = (int *)a; //OK with compiler, but const fade out.
21         y = (int *)b; //OK with compiler, but const fade out.
22         break;
23     case 4:
24         *a = *x; //warning: assignment of read-only location
25         *b = *y; //warning: assignment of read-only location
26     case 5:
27         *x = *a; //OK with compiler, but const fade out.
28         *y = *b; //OK with compiler, but const fade out.
29     default:
30         break;
31     }
32     int result = *a + *b;
33     return result;
34 }

總結以上兩種情況,就是"當const修飾一個普通變量時,則這個普通變量不應被修改。當const修飾一個指針變量時,這個指針指向的內容不應被修改,也不應讓其它指針指向這個內容。"

用const修飾全局變量和局部變量的思想同上。

用static修飾的函數和變量

如果在頭文件x.h中聲明的函數和變量如下

extern static int startPoint;
static int Add(int a, int b);

在源文件x.c中定義如下

1 static int startPoint = 100;
2 static int Move(int a)
3 {
4     return startPoint + a;
5 }

那么這個Move函數就只能在x.c這個源文件中使用。這相當于面向對象里的class里的私有函數了。

用static修飾的變量startPoint,也只能在x.c這個源文件中使用,這相當于面向對象里的class里的私有靜態變量

 

同時,這里也顯示了extern用于聲明全局變量的方法。首先在頭文件x.h里用extern修飾該變量的聲明部分,然后在源文件x.c中定義該變量。

在x.h和x.c里的示例中,我們同時用extern和static修飾了全局變量startPoint,那么這個startPoint變量就只能在源文件x.c中出現了。如果去掉static,那么startPoint就能夠在所有寫了"include "x.h""的源文件中使用了。

 

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()