当前位置:网站首页>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 .
边栏推荐
- 7 row K with the weakest combat effectiveness in the matrix
- Moveit2 -- 2. Quick start of moveit in rviz
- Wechat push - template message parameter configuration
- Redis simple to use
- C programming language (2nd Edition) -- Reading Notes -- 1.5
- 最长上升子序列模型 AcWing 1014. 登山
- Thank you for your likes and attention
- (3) Pass parameters
- C custom set
- 第7章 异常处理
猜你喜欢

Digital triangle model acwing 1018. Minimum toll

Digital triangle model acwing 275. pass note

第13章 IO流

ACM warm-up Exercise 2 in 2022 summer vacation (summary)

第7章 异常处理

Find the combination number acwing 887. find the combination number III

The C programming language (2nd) -- Notes -- 1.6

Introduction to software vulnerability analysis (I)

状态压缩DP AcWing 91. 最短Hamilton路径

Digital triangle model acwing 1027. Grid retrieval
随机推荐
求组合数 AcWing 888. 求组合数 IV
Installation and use of GTEST and gmock
Find the combination number acwing 886. find the combination number II
Game theory acwing 894. Split Nim game
第8章 多线程
Knapsack model acwing 423. Picking herbs
(5) Printf (instead of echo)
Moveit2 - 5. Scenario Planning
Markdown editor syntax - setting of text color, size, font and background color (Reprint)
Opengauss kernel analysis - statistics and row count estimation
2022 Niuke multi school (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
The C programming language (2nd) -- Notes -- 1.8
Find the combination number acwing 887. find the combination number III
FAQs of "relay chain" and "dot" in Poka ecosystem
Raw socket grabs packets, and packets on some ports cannot be caught
First experience of three.js: simulating the growth of a small branch
求组合数 AcWing 887. 求组合数 III
Analysis of distributed database and cache double write consistency scheme (Reprint)
ACM warm-up Exercise 1 in 2022 summer vacation (summary)