typedef用法有哪些
typedef用法有:1、typedef基本資料型別取“別名”,C語言中的所有資料型別都可以用typedef重新定義型別名稱;2、typedef為自訂資料型別取“別名”,自訂的資料型別包括:結構體struct name{ }; 、共用體unit name { };、枚舉enum { };3、typedef為陣列取「別名」;4、typedef為指標取「別名」。
本文的操作環境:Windows10系統、C 20版本、dell g3電腦。
在實際應用中,typedef主要有以下四種用法:
#1)typedef基本資料型別取「別名」
也就是說,C語言中的所有資料型別都可以用typedef關鍵字重新定義型別名稱
typedef unsigned int size;typedef unsigned int16 u16;typedef unsigned int8 u8;...
2)typedef為自訂資料型別取「別名」
自訂的資料型別包括:結構體struct name{ }; 、共用體unit name { };、枚舉enum { };
struct students{ char sex; char name[120]; int ages;};
結構體重新定義資料名稱常用的方法有:
struct students{ char sex; char name[120]; int ages;}std;std.name[20]="wujunwu"
另外也可以用typedef定義:
struct students{ char sex; char name[120]; int ages;};typedef struct students std;std.name[20]="wujunwu"
3)typedef為陣列取「別名」
typedef char arr_name[20];arr_name ane; ane[20]="wujunwu"
4)typedef為指標取「別名」
普通指標
int a=2;int* pointer;pointer =&a;
等同於:
int a=2;typedef int* pointer;pointer p;p=&a;
如果a的資料型別是char ,即char a =2;那麼,
char a=2;typedef int* pointer;pointer p;p=&(pointer)a;
函數指標
typedef unsigned int bool;typedef bool(*pCopySDMMC2Mem)(int, unsigned int, unsigned short, unsigned int*, bool);typedef void (*pBL2Type)(void);pCopySDMMC2Mem p1=(pCopySDMMC2Mem)0xD0037F98;pBL2Type p2 = (pBL2Type)0x23E00000;
其實上面程式執行了兩步驟:
第一步:給指標取「別名」
pCopySDMMC2Mem p1;pBL2Type p2;
第二步:強制型別轉換
(pCopySDMMC2Mem)0xD0037F98; //真正在写代码时不能这样写(pBL2Type)0x23E00000;
第三步:給指標賦值
p1=(pCopySDMMC2Mem)0xD0037F98;p2 = (pBL2Type)0x23E00000;
小結:使用typedef時,typedef並沒有建立任何新類型,它只是為某個已經存在的類型提供一個“別名”,以便在程式中使用。
typedef中的陷阱
接下來看一個簡單的typedef 使用範例,如下面的程式碼所示:
typedef char* PCHAR;int strcmp(const PCHAR,const PCHAR);
在上面的程式碼中,「const PCHAR」 是否相當於「const char*」 呢?
答案是否定的,原因很簡單,typedef 是用來定義一種類型的新別名的,它不同於宏,不是簡單的字串替換。因此,「const PCHAR」中的 const 給予了整個指標本身常數性,也就是形成了常數指標「char* const(一個指向char的常數指標)」。即它實際上相當於“char* const”,而不是“const char*(指向常數 char 的指標)”。當然,要讓const PCHAR 相當於const char* 也很容易,如下面的程式碼所示:
typedef const char* PCHAR;int strcmp(PCHAR, PCHAR);
其實,無論什麼時候,只要為指標宣告typedef,那麼就應該在最終的typedef 名稱中加一個const,以使得該指標本身是常數。
還需要特別注意的是,雖然typedef 並不真正影響物件的儲存特性,但在語法上它還是一個儲存類別的關鍵字,就像auto、extern、static 和register 等關鍵字一樣。因此,像下面這種聲明方式是不可行的:
typedef static int INT_STATIC;
不可行的原因是不能聲明多個儲存類別關鍵字,由於typedef 已經佔據了儲存類別關鍵字的位置,因此,在typedef聲明中就無法再使用static 或任何其他儲存類別關鍵字了。當然,編譯器也會報錯,如在 VC 2010 中的報錯資訊為「無法指定多個儲存類別」。
The difference between typedef and #define:
(1) The symbol name created by typedef is limited to Type, not limited to value
(2) typedef is interpreted by the compiler, not the preprocessor
Four uses of typedef
In practical applications, typedef There are mainly four usages:
1) Typedef basic data types take "aliases"
That is to say, all data types in C language can use typedef Keywords to redefine the type name
typedef unsigned int size;typedef unsigned int16 u16;typedef unsigned int8 u8;...
2) typedef takes an "alias" for the custom data type
Customized data types include: structure struct name{ };, union unit name { };, enumeration enum { };
struct students{ char sex; char name[120]; int ages;};
Commonly used methods for redefining data names for structures are:
struct students{ char sex; char name[120]; int ages;}std;std.name[20]="wujunwu"
In addition, typedef can also be used to define:
struct students{ char sex; char name[120]; int ages;};typedef struct students std;std.name[20]="wujunwu"
3) typedef takes an "alias" for the array
typedef char arr_name[20];arr_name ane; ane[20]="wujunwu"
4) typedef takes an "alias" for the pointer
Ordinary pointer
int a=2;int* pointer;pointer =&a;
is equivalent to:
int a=2;typedef int* pointer;pointer p;p=&a;
If the data type of a is char, that is, char a =2; then,
char a=2;typedef int* pointer;pointer p;p=&(pointer)a;
Function pointer
typedef unsigned int bool;typedef bool(*pCopySDMMC2Mem)(int, unsigned int, unsigned short, unsigned int*, bool);typedef void (*pBL2Type)(void);pCopySDMMC2Mem p1=(pCopySDMMC2Mem)0xD0037F98;pBL2Type p2 = (pBL2Type)0x23E00000;
In fact, the above program performs two steps:
The first step: Give the pointer an "alias"
pCopySDMMC2Mem p1;pBL2Type p2;
The second step: Forced type conversion
(pCopySDMMC2Mem)0xD0037F98; //真正在写代码时不能这样写(pBL2Type)0x23E00000;
Step 3: Assign a value to the pointer
p1=(pCopySDMMC2Mem)0xD0037F98;p2 = (pBL2Type)0x23E00000;
Summary: When using typedef, typedef does not create any new type, it just provides an "alias" for an existing type so that it can be used in used in the program.
Traps in typedef
Next look at a simple typedef usage example, as shown in the following code:
typedef char* PCHAR;int strcmp(const PCHAR,const PCHAR);
In the above code , is "const PCHAR" equivalent to "const char*"?
The answer is no. The reason is very simple. Typedef is used to define a new alias of a type. It is different from a macro and is not a simple string replacement. Therefore, the const in "const PCHAR" gives the entire pointer itself constantness, which forms the constant pointer "char* const (a constant pointer pointing to char)". That is, it is actually equivalent to "char* const", not "const char* (pointer to constant char)". Of course, it is also easy to make const PCHAR equivalent to const char*, as shown in the following code:
typedef const char* PCHAR;int strcmp(PCHAR, PCHAR);
In fact, whenever a typedef is declared for a pointer, it should be in the final typedef name Add a const to make the pointer itself a constant.
It is also important to note that although typedef does not really affect the storage characteristics of the object, it is still a storage class keyword in syntax, just like the keywords such as auto, extern, static and register. . Therefore, the following declaration method is not feasible:
typedef static int INT_STATIC;
The reason why it is not feasible is that multiple storage class keywords cannot be declared, because typedef has already occupied the position of the storage class keyword, so in typedef You can no longer use static or any other storage class keyword in the declaration. Of course, the compiler will also report an error. For example, the error message in VC 2010 is "Cannot specify multiple storage classes."
以上是typedef用法有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C語言數據結構:樹和圖的數據表示與操作樹是一個層次結構的數據結構由節點組成,每個節點包含一個數據元素和指向其子節點的指針二叉樹是一種特殊類型的樹,其中每個節點最多有兩個子節點數據表示structTreeNode{intdata;structTreeNode*left;structTreeNode*right;};操作創建樹遍歷樹(先序、中序、後序)搜索樹插入節點刪除節點圖是一個集合的數據結構,其中的元素是頂點,它們通過邊連接在一起邊可以是帶權或無權的數據表示鄰

文件操作難題的真相:文件打開失敗:權限不足、路徑錯誤、文件被佔用。數據寫入失敗:緩衝區已滿、文件不可寫、磁盤空間不足。其他常見問題:文件遍歷緩慢、文本文件編碼不正確、二進製文件讀取錯誤。

Debian系統中的readdir函數是用於讀取目錄內容的系統調用,常用於C語言編程。本文將介紹如何將readdir與其他工具集成,以增強其功能。方法一:C語言程序與管道結合首先,編寫一個C程序調用readdir函數並輸出結果:#include#include#includeintmain(intargc,char*argv[]){DIR*dir;structdirent*entry;if(argc!=2){

算法是解決問題的指令集,其執行速度和內存佔用各不相同。編程中,許多算法都基於數據搜索和排序。本文將介紹幾種數據檢索和排序算法。線性搜索假設有一個數組[20,500,10,5,100,1,50],需要查找數字50。線性搜索算法會逐個檢查數組中的每個元素,直到找到目標值或遍歷完整個數組。算法流程圖如下:線性搜索的偽代碼如下:檢查每個元素:如果找到目標值:返回true返回falseC語言實現:#include#includeintmain(void){i

如何在 C 語言中輸出倒數?回答:使用循環語句。步驟:1. 定義變量 n 存儲要輸出的倒數數字;2. 使用 while 循環持續打印 n 直到 n 小於 1;3. 在循環體內,打印出 n 的值;4. 在循環末尾,將 n 減去 1 以輸出下一個更小的倒數。

C語言多線程編程指南:創建線程:使用pthread_create()函數,指定線程ID、屬性和線程函數。線程同步:通過互斥鎖、信號量和條件變量防止數據競爭。實戰案例:使用多線程計算斐波那契數,將任務分配給多個線程並同步結果。疑難解答:解決程序崩潰、線程停止響應和性能瓶頸等問題。

C語言函數包含定義、調用和聲明。函數定義指定函數名、參數和返回類型,函數體實現功能;函數調用執行函數並提供參數;函數聲明告知編譯器函數類型。值傳遞用於參數傳遞,注意返回類型,保持一致的代碼風格,並在函數中處理錯誤。掌握這些知識有助於編寫優雅、健壯的C代碼。

整數是編程中最基礎的數據類型,堪稱編程的基石。程序員的工作就是賦予這些數字意義,無論軟件多麼複雜,最終都歸結於整數運算,因為處理器只理解整數。為了表示負數,我們引入了二進制補碼;為了表示小數,我們創造了科學計數法,於是有了浮點數。但歸根結底,一切仍然離不開0和1。整數的簡史在C語言中,int幾乎是默認類型。儘管編譯器可能會發出警告,但在許多情況下,你仍然可以寫下這樣的代碼:main(void){return0;}從技術角度來看,這與以下代碼等效:intmain(void){return0;}這種