Explanation of typedef in C Language

Source: Internet
Author: User

Typedef can be seen as an abbreviation of type define. as its name implies, it is a type definition. That is to say, it only re-defines an easy-to-use alias for an existing type and does not generate a new data type.

The use of typedef is somewhat similar to that of macro definition define, but the two have the following differences:

1. Unlike # define, the symbol name given by typedef is limited to the type rather than the value.

2. typedef is interpreted by the compiler rather than the Preprocessor.

3. typedef is more flexible than # define.

Since typedef does not define a new data type, why should we use it? Typedef has its own advantages:

1. It makes the definition more intuitive, and you can understand some information about the variable from the definition. For example

Typedef unsigned int BYTE;

BYTE x, y [10], * z;

2. It can parameterize programs to improve program portability. For example

Time_t time (time_t *); this function returns a return value of the time_t type. In some systems, time_t is defined as the unsigned long type. In other systems, it may be defined as the unsigned int type. In this way, when porting data to different systems, you only need to change the typedef definition and port the data in different systems.

3. The expression is more concise. For example

When a struct is named using typedef, typedef struct {double x; double y ;}rect; rect r1 = {3.0, 6.0 };

If typedef is not used, it is complex. struct {double x; double y;} r1 = {3.0, 6.0 };


The scope defined by typedef depends on the position of the typedef statement. If it is defined within a function, its scope is local and limited to that function. If it is defined outside the function, it will have a global scope. The type declared in typedef appears at the position of the variable name, instead of following the keyword typedef. Typedef syntax is similar to extern and static in the storage class, so typedef and static cannot be used for a variable type at the same time. After creating a data type name, you can use it for type declaration and type conversion. For example:

Typedef char * String;

String p, lineptr [MAXLINES], alloc (int); // type declaration

Int strcmp (String, String );

P = (String) malloc (100); // type conversion


Common examples of typedef are as follows:

1. Define the alias of a variable.

Typedef char * PChar;

PChar a, B; // equivalent to char * a; char * B;

2. Combined Use with struct.

Typedef struct Node {

Int;

Char * B;

} * PNode;

PNode a, B;

3. Define a type alias for a complex variable.

The following is a reference to other blogs:

Typedef int (* PF) (const char *, const char *); this declaration introduces the PF type as the synonym of the function pointer, this function has two parameters of the const char * type and a return value of the int type. 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. For example:

Void (* signal (int signr, void (* handler) (int); can be defined by typedef twice.

Typedef void sigfunc (int );

Sigfunc * signal (int signr, sigfunc * handler );

Typedef defines a function type with an integer parameter that does not return a value. Void (* handler) (int) indicates a function pointer with no return value for an integer parameter. This pointer is named handler, so it can be described using sigfunc, in this case, sigfunc is equivalent to the int function in the previous int signr. Similarly, this function is also used.

Understand the "right-left rule" available for complex statements: Starting from the variable name, you must first go to the right and then to the left. When you encounter a parentheses, you can adjust the reading direction. After analyzing the parentheses, you can jump out of the brackets, it is still in the order of the first right and then the left, until the entire declaration analysis is complete. Example:
Int (* func) (int * p );
First, find the variable name func, and there is a pair of parentheses on the outside, and there is a * sign on the left, which indicates that func is a pointer. Then jump out of the parentheses and look at the right first, this indicates that (* func) is a function, so func is a pointer to this type of function, that is, a function pointer. This type of function has an int * type parameter, and the return value type is int.
Int (* func [5]) (int *);
The right side of func is a [] Operator, indicating that func is an array with five elements; there is a * on the left of func, indicating that the func element is a pointer (note that * is not a modifier of func, instead, modify func [5] Because the [] operator has a higher priority than *, and func is first combined ). Jump out of the brackets and look at the right side. The parentheses indicate that the element of the func array is a pointer of the function type. It points to an int * type parameter and the return value type is int.
Http://blog.csdn.net/shadow_gz/article/details/4326106

Http://blog.163.com/njut_wangjian/blog/static/165796425201232510265243/


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.