A typedef-based explanation of "turn"

Source: Internet
Author: User

Perhaps the novice with this keyword is not many, but it is a very useful keyword, you can make the code more modular (that is, less association with other code), in C + + is also the basis of implementation of traits technology, is also one of the basic syntax of template programming.

If the variable definition is named for a variable, and a typedef (or type definition) is named for the type. Since all are named, there will be a lot of similar places. And the variable definition I think everyone will use, so the type definition will certainly be used.

The syntax of a type definition boils down to a sentence: As long as you precede the variable definition with a TypeDef, you become a type definition. The thing that is supposed to be a variable here, it becomes the type.
For example, the following variables are defined:
int integer; Integer variable
int *pointer; Integer pointer variable
int array [5]; Integer array variable
int *p_array [5]; Variable of an array of integer pointers
int (*array_pointer) [5];//variable for pointer to integer array
int function (int param);//function definition, function name can also be considered as a variable of a function
int *function (int param);//is still a function, but the return value is an integer pointer
Int (*function) (int param);//Now is the pointer to the function
To define the appropriate type, you name the type, which is the following form:
typedef int integer_t; Integral type
typedef int *pointer_t; Integer pointer type
typedef int ARRAY_T [5]; Integer array type
typedef int *P_ARRAY_T [5]; Type of an array of integer pointers
typedef int (*array_pointer_t) [5]; Type of pointer to an integer array
typedef int function_t (int param); function type
typedef int *function_t (int param); function type
typedef int (*function_t) (int param); The type of pointer to the function
Note: The above function type can be faulted in C because there is no function type in C, and its function variables automatically degenerate into function pointers; it seems to be possible in C + +. The main explanation here is the formal similarity.

The general form of a typedef is:
typedef type definition name;
The purpose of using TypeDef in programming is generally two, one is to give the variable a new name that is easy to remember and meaning, and the other is to simplify some more complex type declarations.
In fact, when declaring a variable in the C language, there is a storage type indicator (Storage-class-specifier), which includes our familiarextern, static, auto, register。 When you do not specify a storage type indicator, the compiler automatically takes the default values according to the conventions. In addition, the location of the store type indicator is arbitrary (but before the variable name and pointer *), which means that the following lines of code are equivalent:
static const int i;
const static int i;
int const static I;
const int static i;
According to the C language Specification,typedef and storage type indicators are equivalent for syntactic analysis! Therefore, we replace the above-used static with a typedef:
typedef const INT I;
const typedef int i;
int const typedef i;
const int typedef i;
The semantics of the preceding code is that I is defined as a type name with an equivalent type of const int. Later, if we have the I-a code, it is equivalent to the const int A. The same is true for places where pointers are available, such as:
The int const typedef *T; Then the code T p. is equivalent to the int const *p.
In addition, typedef cannot be used in conjunction with storage type indicators such as static, because each variable can have only one storage type, so the code: typedef static INT i;
Using typedef to simplify complex variable declarations
1), define an array with 10 pointers pointing to a function that has a shaping parameter and returns an integer type?
First method: Int (*a[10]) (int);
The second method: TypeDef int (*PFUNC) (int);
Pfunc A[10];
2), define an array with 10 pointers to a function that has a function pointer (with no parameters, a null return value), and returns NULL.
First method: void (*a[10]) (void (*) (void));
The second method: typedef void (*pfuncparam) (void);
typedef void (*PFUNC) (Pfuncparam);
Pfunc A[10];
3), a pointer to an array of 10 function pointers (with no arguments, with a return value of double)
The first method: Double (*) (void) (*P) [10];
The second method: TypeDef double (*PFUNC) (void);
typedef pfunc (*pfuncparam) [10];
Pfuncparam p;
Summary:
There are two uses of typedef:
I. General form, define an alias of an existing type
typedef type definition name;
Second, create a new type
typedef returns a new type name for the value type (parameter list);

A typedef-based explanation of "turn"

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.