首頁  >  文章  >  typedef用法有哪些

typedef用法有哪些

DDD
DDD原創
2023-06-07 10:38:105075瀏覽

typedef用法有:1、typedef基本資料型別取“別名”,C語言中的所有資料型別都可以用typedef重新定義型別名稱;2、typedef為自訂資料型別取“別名”,自訂的資料型別包括:結構體struct name{ }; 、共用體unit name { };、枚舉enum { };3、typedef為陣列取「別名」;4、typedef為指標取「別名」。

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn