当前位置:网站首页>The C programming language 2nd -- Notes -- 6.7
The C programming language 2nd -- Notes -- 6.7
2022-07-27 11:32:00 【YovaVan】
6.7 The type definition
C Yes typedef function , Used to create a new data type name . Such as , Statement
typedef int Length;take Length Defined as and int Names with the same meaning . type Length Can be used for type declarations 、 Type conversion, etc , It and type int Exactly the same , Such as :
Length len, maxlen;
Length *Lengths[];Similarly , Statement
typedef char* String;/* chinese */typedef char *String;/* english */take String Defined as char * Or character pointer , It can then be used in type declarations and type conversions String, for example :
String p, lineptr[MAXLINES], alloc(int);
int strcmp(String, String);
p = (String) malloc(100);
Be careful , typedef The type declared in the variable name appears in the variable name position , Not immediately after the keyword typedef after . typedef It is similar to storage class in syntax extern、 static etc. . Use capital letters here typedef The first letter of the name of the defined type , In order to show the difference between .
Take a more complicated example : use typedef Define the tree nodes described earlier in this chapter . as follows :
typedef struct tnode *Treeptr;
typedef struct tnode { /* The tree node:*/
char *word; /* Points to the text*/
int count; /* Number of the occurrences*/
struct tnode *left /* Left child*/
struct tnode *right /* Right child*/
} treenode;The above type definition creates two new type keywords : Treenode( A structure ) and Treeptr( A pointer to the structure ). such , function talloc It can be modified as :
Treeptr talloc(void)
{
return (treeptr) malloc(sizeof(treenode));
}emphasize , typedef The declaration does not create a new type , It just adds a new name to an existing type .typedef The declaration does not add any new semantics : Variables declared in this way have exactly the same properties as variables declared in the normal way . actually ,typedef Be similar to #define sentence , But because of typedef Is interpreted by the compiler , Therefore, its text replacement function exceeds the capacity of the preprocessor . for example :
typedef int (*PFI) (char *, char *); This statement defines the type PFI yes “ A pointer to a function , This function has two char * Parameters of type , The return value type is int”, It can be used in certain contexts , for example , Can be used on page 5 In the sorting program in Chapter , as follows :
PFI strcmmp, numcpm;In addition to more concise expression , Use typedef There are two other important reasons . First , It can parameterize the program , To improve the portability of the program . If typedef The declared data type is machine dependent , When the program is transplanted to other machines , Only need to change typedef Just define the type . A frequently used case is , For shaping values of various sizes , All through typedef Defined type name , then , Select a set of appropriate hosts for each host short、int、 and long Type size is enough . There are some examples in the standard library , for example size_t and ptrdiff_t etc. .
typedef Of the 2 One function is to provide better explanation for the program ——Treeptr A type is obviously easier to understand than a pointer declared to a complex structure .
边栏推荐
- 背包模型 AcWing 1022. 宠物小精灵之收服
- An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
- 记忆化搜索 AcWing 901. 滑雪
- 349 sum of intersection of two arrays and 01
- 数字三角形模型 AcWing 1018. 最低通行费
- WGet warning: unable to verify
- Thank you for your likes and attention
- 11 wrong set
- (5) Printf (instead of echo)
- Knapsack problem acwing 9. grouping knapsack problem
猜你喜欢

最长上升子序列模型 AcWing 1012. 友好城市

Knapsack problem acwing 9. grouping knapsack problem

Moveit2 - 4. robot model and robot state

Redis simple to use

栈 AcWing 3302. 表达式求值

求组合数 AcWing 887. 求组合数 III

区间问题 AcWing 906. 区间分组

数字三角形模型 AcWing 1018. 最低通行费

Knapsack model acwing 1024. Packing problem

Solutions to errors in tensorflow operation
随机推荐
Win10 vscode code code format setting and remote breakpoint debugging
数字三角形模型 AcWing 1027. 方格取数
高斯消元 AcWing 883. 高斯消元解线性方程组
349 sum of intersection of two arrays and 01
Find the combinatorial number acwing 889. 01 sequence satisfying the condition
Interval problem acwing 906. Interval grouping
Raw socket grabs packets, and packets on some ports cannot be caught
9 UAV array
C custom set
2022牛客多校 (3)J.Journey
Error while unzipping file in win10: unable to create symbolic link. You may need to run WinRAR with administrator privileges. The client does not have the required privileges
Internal and external troubles of digital collection NFT "boring ape" bayc
Digital triangle model acwing 275. pass note
第13章 IO流
Today's code farmer girl learned notes about event operation and ref attribute, and did the practice of form two-way binding
Backpack model acwing 1022. Collection of pet elves
数字三角形模型 AcWing 1018. 最低通行费
中国剩余定理 AcWing 204. 表达整数的奇怪方式
49字母异位分组和242有效的字母异位词
Lazy loading of lists and pictures