In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the function of typedef keywords in C language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the function of the C language typedef keyword?"
1. Make a joke.
Zhao Benshan had such a joke at the Spring Festival Gala. It goes like this.
A tiger was bitten by a snake. The tiger was anxious and wanted to trample the snake to death and chase it to a small river. the snake drilled into the water, and the tiger was just waiting on the river bank. The tiger said, "I don't believe you won't come out." After a while, a son of a bitch came out of it, and the tiger went up and held it down. "I don't know you if you wear a vest?"
This is the little turtle.
Typedef keyword is good at doing tricks, any other type of encounter with him can be transformed into another look, such a powerful keyword, we must supply, because it is so awesome.
2. Typedef or should be typealias
Typedef literally means type + define means to redefine the data type, but this understanding is not correct. We are learning Linux and know that there is a word called alias, which means an alias. It would be better to understand type as typealias. For example, Yao Ming, we named him a giant named China, or Kobe Bryant, we gave him a name called Black Mamba.
When we are working on a project, we need to put a lot of data types together and then come up with a new name, which requires typedef, for example:
Typedef struct player
{
/ / code
} Play_st,*Play_p
A), struct player play1; and Play_st play1; are the same
B), struct player * play2; and Play_p play2; Play_st * play2; are the same
We don't know if we have any doubts about B's answer. We can understand it this way. Typedef AAAA BBBB; defines the alias of AAAA as BBBB, so we can understand it this way.
The alias of struct player {/ / code} is Play_st
The alias of struct player {/ / code} * is Play_p
3. Typedef and storage class keywords (storage class specifier)
Isn't it a bit surprising that typedef, like auto,extern,mutable,static, and register, is a storage keyword. This is not to say that typedef really affects the storage properties of objects; it just says that in terms of statement composition, typedef declarations look like variable declarations of types such as static,extern. Here is a trap:
Typedef static int FAST_COUNTER; / / error
The compiler won't pass. The problem is that you cannot have more than one storage class keyword in the declaration. Because the symbol typedef already occupies the location of the storage class keyword, static (or any other storage class keyword) cannot be used in the typedef declaration.
4. Use typedef to define platform-independent types
For example, define a floating-point type called REAL. On the target platform 1, let it represent the type with the highest precision:
Typedef long double REAL
On platform 2 that does not support long double, change to:
Typedef double REAL
On platform 3 that does not even support double, change to:
Typedef float REAL
In other words, when cross-platform, just change the typedef itself, without any changes to other source code.
This technique is widely used in standard libraries, such as size_t. In addition, because typedef defines a new type of alias, not a simple string substitution, it is more robust than macros.
This advantage in the process of writing code can reduce a lot of code, as embedded development students, for their own driver code, if the code is well written, it is very easy to transplant to other platforms at the same time.
5. Define a new and simple alias for complex declarations
The way to do this is to gradually replace part of the complex declaration with an alias in the original declaration, so that the part with the variable name is left for the last replacement, and the simplest version of the original declaration is obtained.
Example 1:
Original statement: void (* b [10]) (void (*) ())
The variable name is b. Replace the parentheses on the right first, and pFunParam is alias one:
Typedef void (* pFunParam) ()
Then replace the variable b _
Typedef void (* pFunx) (pFunParam)
The simplest version of the original statement:
PFunx b [10]
Example 2:
Original declaration: doube (*) () (* e) [9]
The variable name is e, replace the left part first, and pFuny is alias one:
Typedef double (* pFuny) ()
Then replace the variable eDiary pFunParamy on the right to alias two
Typedef pFuny (* pFunParamy) [9]
The simplest version of the original statement:
PFunParamy e
Understand the "right-left rule" available for complex declarations: starting from the variable name, first to the right, then to the left, encounter a parenthesis
Just turn the direction of reading; after the analysis in parentheses, jump out of parentheses, or in the order of right then left, so circular, straight
By the time the whole statement is analyzed.
For example:
Int (* func) (int * p)
First find the variable name func, there is a pair of parentheses outside, and the left is a * sign, which means that func is a pointer; then jump out of the parentheses, first look at the right, and then encounter parentheses, which means (* func) is a function, so func is a pointer to this type of function, that is, function pointer, this kind of function has a parameter of type int*, and the return value type is int.
Int (* func [5]) (int *)
To the right of func is a [] operator, indicating that func is an array of five elements; to the left of func, there is a *, indicating that the element of func is a pointer. (note that the * here does not modify func, but func [5] because the [] operator has a higher priority than *, and func is combined with [] first.) Jump out of this parenthesis, look to the right, and encounter parentheses, indicating that the element of the func array is a pointer to the function type, which points to a function with a formal parameter of type int*, and the return value is of type int.
This kind of usage is quite complex, appears frequently also, often sees this kind of usage but cannot understand, believed that the above explanation can be helpful.
6. The difference between typedef and # define
Case 1:
Generally speaking, typedef is better than # define, especially when there is a pointer. Take a look at the example:
Typedef char * pStr1
# define pStr2 char *
PStr1 s1, s2
PStr2 s3, s4
In the above variable definition, S1, S2, S3 are all defined as char *, while S4 is defined as char, not us.
The root cause of the expected pointer variable is that # define is a simple string substitution and typedef is one.
Give a new name to the type.
Case 2:
The compiler will report an error in the following code. Do you know which statement is wrong?
# include "stdio.h"
Void main (void)
{
Typedef char * pStr
Char string [4] = "abc"
Const char * p1 = string
Const pStr p2 = string
P1cm +
P _ 2cm +
}
It is a mistake made by p2percent +. Once again, this question reminds us that typedef, unlike # define, is not a simple text replacement. Const pStr p2 is not equal to const char * p2 in the above code. There is essentially no difference between const pStr p2 and const long x, both of which impose read-only restrictions on variables, except that the data type of variable p2 is defined by us rather than inherent in the system. Therefore, const pStr p2 means that the variable p2 with a data type of char * is read-only, so p2 contains + error.
So the question is, why is there a const in front of p1roomplus, no problem?
# include "stdio.h"
Void main (void)
{
Typedef char * pStr
Char string [4] = "abc"
Const char * p1 = string
Const pStr p2 = string
P1cm +
/ / p2percent +
Printf ("% s\ n", p1)
}
This involves the following concept
/ / the positions of const and type can be interchangeable, but not at will if the type is a pointer.
For example:
Const int i; and int const i; are the same.
So let's take a look at the above two sentences.
Typedef char * pStr
Char string [4] = "abc"
Const char * p1 = string; / / p1 is a pointer. The value that p1 points to is of type const char, but the value of p1 can be changed.
Const pStr p2 = string; / / pStr itself is a data type, you can now replace pStr with int, where const limits that the value of p2 cannot be changed, so p2notify + makes an error.
Let's change the above code to the following
# include "stdio.h"
Void main (void)
{
Typedef char * pStr
Char string [4] = "abc"
Char * const p1 = string
Const pStr p2 = string
P1cm +
/ / p2percent +
Printf ("% s\ n", p1)
}
At this point, I believe you have a deeper understanding of "what is the role of the C language typedef keyword". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.