当前位置:网站首页>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 423. 采药
- 记忆化搜索 AcWing 901. 滑雪
- Moveit2 -- 2. Quick start of moveit in rviz
- Miscellaneous records of Finance
- 2022 Niuke multi school training (3) a-ancestor topic translation
- 解决 ImportError: cannot import name 'abs' 导入tensorflow报错
- (9) Shell I / O redirection
- Find the combinatorial number acwing 889. 01 sequence satisfying the condition
- 深析C语言的灵魂 -- 指针
- 2022 Niuke multi school (3) j.journey
猜你喜欢

Game theory acwing 891. Nim game

PAT(乙级)2022年夏季考试

Caused by:org.gradle.api.internal. plugins . PluginApplicationException: Failed to apply plugin

第13章 IO流

Knapsack model acwing 1024. Packing problem

What is the mystery of the gate of the meta universe?

Solve importerror: cannot import name'abs'import tensorflow error

Find the combination number acwing 886. find the combination number II

49 letter ectopic grouping and 242 effective letter ectopic words

Instructions for mock platform
随机推荐
求组合数 AcWing 888. 求组合数 IV
最长上升子序列模型 AcWing 1012. 友好城市
01 BTC cryptology principle
树形DP AcWing 285. 没有上司的舞会
C programming language (2nd Edition) -- Reading Notes -- 1.5.2
Properties file
最长上升子序列模型 AcWing 1016. 最大上升子序列和
I've compromised. Since everyone wants to call me Yelin, there's nothing I can do
What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
(9) Shell I / O redirection
数字三角形模型 AcWing 275. 传纸条
Introduction to software vulnerability analysis (I)
求组合数 AcWing 885. 求组合数 I
Remember an experience of using canvas to make the banner streamer effect of Tencent cloud homepage
Sorry, you guys have something to deal with in the bank recently, which has been delayed
[shader realizes shake random shaking effect _shader effect Chapter 10]
第12章 泛型
Backpack model acwing 1022. Collection of pet elves
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
FAQs of "relay chain" and "dot" in Poka ecosystem