Usage of typedef in C Language

Source: Internet
Author: User
Tags define definition

1. Basic explanation
Typedef is a key word in C language. It defines a new name for a data type. The data types here include internal data types (int, char, etc.) and Custom Data Types (struct, etc ).
In programming, typedef is generally used for two purposes. One is to give a variable a new name that is easy to remember and clear, and the other is to simplify some complicated type declarations.
As for the subtlety of typedef, let's take a look at the specifics of several problems.
2. typedef & Structure Problems
When the following code defines a structure, the compiler reports an error. Why? Does the C language not allow the structure to contain pointers to itself? First, let's look at the following description:
Typedef struct tagNode
{
Char * pItem;
PNode pNext;
} * PNode;
Answer and analysis: www.2cto.com
1. The simplest use of typedef
Typedef long byte_4;
Give a new name for the known data type long, called byte_4.
2. Use typedef in combination with the structure
Typedef struct tagMyStruct
{
Int iNum;
Long lLength;
} MyStruct;
This statement actually completes two operations:
1) define a new structure type
Struct tagMyStruct
{
Int iNum;
Long lLength;
};
Analysis: tagMyStruct is called "tag", that is, "tag". It is actually a temporary name. The struct keyword and tagMyStruct constitute this structure type, whether or not there is typedef, this structure exists.
We can use struct tagMyStruct varName to define variables, but note that it is incorrect to use tagMyStruct varName to define variables, Because struct and tagMyStruct can be combined to represent a structure type.
2) typedef is named MyStruct for the new structure.
Typedef struct tagMyStruct MyStruct; therefore, MyStruct is actually equivalent to struct tagMyStruct. We can use MyStruct varName to define variables.
Answer and Analysis
C language certainly allows a pointer to its own in the structure. We can see countless such examples in the implementation of data structures such as the establishment of a linked list. The fundamental problem of the above Code lies in the application of typedef.

According to the above description, we can know that the pNext domain declaration is encountered during the creation of the new structure. The type is pNode. You must know that pNode represents the new name of the type, when the type itself is not created, the new name of the type does not exist. That is to say, the compiler does not know pNode at this time.
There are many ways to solve this problem:
1) typedef struct tagNode
{
Char * pItem;
Struct tagNode * pNext;
} * PNode;
2) typedef struct tagNode * pNode;
Struct tagNode
{
Char * pItem;
PNode pNext; // pointer type
};
Note: In this example, you use typedef to give a new name for a type that is not fully declared. C language compiler supports this approach.
3) standard practices:
Struct tagNode
{
Char * pItem;
Struct tagNode * pNext;
};
Typedef struct tagNode * pNode;
3. typedef & # define
There are two methods to define the pStr data type. What are the differences between the two? Which one is better? Typedef char * pStr;
# Define pStr char *;
Answer and analysis:
Generally, typedef is better than # define, especially when there is a pointer. See the example: typedef char * pStr1;
# Define pStr2 char *
PStr1 s1, s2;
PStr2 s3, s4;
In the preceding variable definition, s1, s2, and s3 are both defined as char *, while s4 is defined as char, which is not the expected pointer variable, the root cause is that # define is a simple string replacement, while typedef is a new name for a type.
In the preceding example, the define statement must be written as pStr2 s3, * s4; so that the statement can be executed normally.
# Define usage example:
# Define f (x) x * x
Main ()
{Int a = 6, B = 2, c; c = f (a)/f (B); printf ("% d \ n", c );} the output result of the following program is: 36.
For this reason, when # define definition is used in many C language programming specifications, if the definition contains expressions and must use parentheses, the above definition should be as follows:
# Define f (x) (x * x)
Of course, if you use typedef, there is no such problem.
4. Another example of typedef & # define
In the following code, the compiler reports an error. Do you know which statement is wrong?
Typedef char * pStr;
Char string [4] = "abc ";
Const char * p1 = string;
Const pStr p2 = string;
P1 ++; p2 ++;
Answer and analysis:
P2 ++ error. Another reminder is that typedef is different from # define, which is not a simple text replacement. In the above Code, const pStr p2 is not equal to const char * p2. There is no difference between const pStr p2 and const long x in nature. They both impose read-only restrictions on variables, except that the data type of the variable p2 is defined by ourselves rather than the inherent type of the system. Therefore, const pStr p2 indicates that the variable p2 with the Data Type limited to char * is read-only, so p2 ++ is incorrect.
# Define and typedef Extension
1) # define macro definition has a special advantage: You can use # ifdef, # ifndef, and so on for logical judgment. You can also use # undef to cancel definition.
2) typedef also has a special advantage: It complies with range rules, the type of a variable defined by typedef is restricted to the defined function or file (depending on the location of the variable definition), while the macro definition does not.
5. typedef & Complex Variable Declaration
In programming practices, especially when looking at other people's code, we often encounter complicated variable declarations and use typedef to simplify our own values, such:
The following is the declaration of three variables. I want to use typdef to define an alias for them. What should I do?
> 1: int * (* a [5]) (int, char *);
> 2: void (* B [10]) (void (*)());
> 3. double (*) (* pa) [9];
Answer and analysis:
The method to create a type alias for a complex variable is very simple. You only need to replace the variable name with the type name in the traditional variable declaration expression, and then add the keyword typedef to the beginning of the statement.
> 1: int * (* a [5]) (int, char *);
// PFun is a type alias we created
Typedef int * (* pFun) (int, char *);

// Declare an object using the new defined type, equivalent to int * (* a [5]) (int, char *);
PFun a [5];
> 2: void (* B [10]) (void (*)());

// First declare a new type for the blue part of the above expression

Typedef void (* pFunParam )();

// Declare a new type as a whole

Typedef void (* pFun) (pFunParam );

// Declare the object using the new defined type, which is equivalent to void (* B [10]) (void (*)());
PFun B [10];
> 3. double (* pa) [9]) ();

// First declare a new type for the blue part of the above expression
Typedef double (* pFun )();
// Declare a new type as a whole
Typedef pFun (* pFunParam) [9];
// Declare an object using the new defined type, which is equivalent to double (* pa) [9]) ();
PFunParam pa;

 

By AlphaJay

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.