In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use typedef in C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Typedef is often used in single-chip computers and operating systems. It can customize the name for a certain type. Similar to # define. But there are differences.
Symbols created by typedef can only be used for data types, not values. The symbols created by # define can be used for values.
Typedef is interpreted by the compiler, not the preprocessor.
Typedef is more flexible to use.
Let's use typedef to define a data type
Int main () {typedef unsigned char BYTE; BYTE c = 10; printf ("% d\ r\ n", c); system ("pause"); return 0;}
Use typedef to redefine the unsigned char type as the BYTE type, and then define a variable c using the new type BYTE. The output is as follows:
If you use # define to replace it at this time, it seems to be OK.
It also seems correct to use # define to replace unsigned char with BYTE.
Let's look at an example.
Int main () {typedef char * STR; STR S1 instruction s2; S1 = "abc"; S2 = "123"; printf ("% s\ r\ n", S1 Magi S2); system ("pause"); return 0;}
Redefine the character pointer char * to type STR, and then use STR to define two pointers S1 and S2, so that S1 and S2 are pointers to characters. Then assign values to these two pointers and print them out.
At this point, use # define to replace the typedef function.
# define STR char * int main () {STR S2; S1 = "abc"; S2 = "123"; printf ("% s\ r\ n", S1 Persons2); system ("pause"); return 0;}
When compiling the program at this time, the program will report an error.
Why is it now? Because when using typedef, a new data type is defined.
Typedef char * STR; STR S1 and S2
Equivalent to
Char * S1, * s2trait S1 = "abc"; S2 = "123"
S1 and S2 are pointers to character types.
If you use # define, the system is simply replaced.
# define STR char * STR S1 and S2
Replace STR with char *
Char * S1 Persons2; S1 = "abc"; S2 = "123"
After replacement, only S1 is the pointer, and S2 is still a variable of type char. So the system will report an error when assigning a string to S2. So when you want to redefine a data type, you use typedef, which is used a lot when defining structures.
Typedef struct Date {int year; int month; int day;} DT
Use typedef to redefine the structure struct Date as a DT type, so that when you use DT to define variables, it is equivalent to using struct Date to define variables.
Typedef struct Date {int year; int month; int day;} DT;DT D1 = {2021 10Jet 1}; DT D2 = {2021 Jet 10Jet 10}
Equivalent to
Struct Date {int year; int month; int day;}; struct Date D1 = {2021 ~ (10) Jol 1}; struct Date D2 = {2021 ~ (10) Jing 10}
This makes the code more concise by using typedef to redefine a new type. This method is most widely used in the library function of single-chip microcomputer.
Typedef struct {uint16_t GPIO_Pin; GPIOSpeed_TypeDef GPIO_Speed; GPIOMode_TypeDef GPIO_Mode;} GPIO_InitTypeDef;void LED_Init (void) {GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; / / LED0-- > PA8 port configuration GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP / / push-pull output GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; / / IO port speed is 50MHz GPIO_Init (GPIOA, & GPIO_InitStructure); / PD2 output high}
The structure weight is defined as the GPIO_InitTypeDef type through typedef, so that a structure about GPIO can be defined by using the GPIO_InitTypeDef type directly during initialization.
When defining a data type, typedef is also used to redefine a data type.
Defining new data types through tepedef increases the readability and portability of the code. Instead of creating a data type that does not exist in the system, the new data type here adds an easy-to-use label to a type that already exists. And this tag is different from # define, it's just a simple symbol replacement.
This is the end of the content of "how to use typedef in C language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.