A typedef declaration, called a TypeDef, creates a new name for an existing type. For example, people often use a typedef to write more beautiful and readable code. The so-called aesthetics, meaning that the typedef can hide clumsy syntax constructs and platform-related data types, thereby enhancing portability and future maintainability.
There are two commonly used typedef in programming, one is to give variables a new name that is easy to remember and meaningful, and the other is to simplify some of the more complex type declarations.
is to learn the data structure of the time, found that before the knowledge of learning to forget a lot, in the discovery of the key word in C + + in the understanding of the TypeDef or not in place, I read the learning C + + used textbooks, and asked the Niang, but also read a lot about the use of TypeDef blog So I wanted to sort out what I understood.
I. Basic EXPLANATIONS
A typedef is a keyword in the C language that 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.).
There are two commonly used typedef in programming, one is to give variables a new name that is easy to remember and meaningful, and the other is to simplify some of the more complex type declarations.
Two Usage
(1) Use typedef to declare a new type name in place of the existing type name. Such as:
typedef int STATUS//Specify identifier Status on behalf of int type
typedef double DATE//Specify identifier date to represent the double type
So the following code is equivalent:
int i; Double J;
Status I;date J;
(2) The new name is used for the array type of typedef:
typedef int NUM[100];//declares num to be an integer array type, can contain 100 elements
num n;//definition n is an array containing 100 integer elements, n is the array name
(3) Declare a new name for a struct type:
The typedef struct//struct is preceded by a keyword typedef, which means declaring the new type name
{
int month;
int day;
int year;
} time; Time is a new type name, but is not a new type, nor is it a struct variable name
The newly declared new type name time represents a struct type specified above, so that the struct variable can be defined with the, such as:
Time birthday;
Time *p//p is a pointer to the type of data in the struct body
Three Pay attention.
(1) using typedef only adds a type name to an existing type without creating a new type. Just add a new name, you can use the name to define the variable, such as using the status above to define the variable I, then the type of the I variable is int type.
(2) The new type name can be declared with typedef. But cannot be used to define variables
Four Advantages
The use of a TypeDef type name facilitates the portability of programs. Sometimes programs rely on hardware features. For example, in a C + + system with 2 bytes to save an int type variable, with 4 bytes to save a long type variable. In another C + + system, an int type variable is stored in 4 bytes. When porting a C + + program from a C + + system with 2 bytes of an int type variable to a C + + system that holds an int type variable in 4 bytes, if the type of int is declared with TypeDef, for example:
Typedef int INTEGER; This is the original write
Typedef long INTEGER, and/or after transplantation can be changed to this
If it is not declared with a TypeDef, then each place that defines the type int is changed, and the larger the program, the greater the workload.
The above is a small set up to introduce C and C + + in the simple use of the TypeDef, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for your support cloud Habitat community!