Simplified codeTo promote cross-platform development.
Typedef acts a bit like # define macro and replaces the synonym with its actual type.
Difference: typedefInterpreted during compilationSo let the compiler handleText replacement beyond pre-processor capabilities.
Usage 1:
Typedef int(* Myfun)(INT, INT);
This method is generally usedFunction Definition aliasTime
The example above definesMyfunIsFunction pointerThe function type includes two int parameters, and an int is returned.
InAnalysisWhen defining this form, you can use the following method:
Remove typedef and alias first,The rest is the type of the original variable.
Remove typedef and myfun:
INT (*) (INT, INT)
Usage 2:
Typedef defines a variable typeAlias.
TypedefStruct {
Int;
Int B;
}My_type;
Here, an unnamed structure is taken directly as an alias named my_type, so that if you want to define the structure of the instance, you can do this:
My_type TMP;
Second usage: typedef Original variable type Alias
Simple function pointer usage
// Form 1: return type (* function name) (parameter table)
Char (* pfun) (INT );
//Typedef Char (*Pfun) (INT) // the same function as the previous line
/* The function of typedef isDefine a New Type. The first sentence isDefines a ptrfun typeAnd define this typePointer to a functionThis type of function takes an int as the parameter and returns the char type. */
Char glfun (int A) {return ;}
Void main ()
{
Pfun = glfun;
(* Pfun) (2 );
}
The first line defines a pointer variable.Pfun. It is a pointer to a function. This function parameter is of the int type and the return value is of the char type.We cannot use this pointer only in the first sentence, because we have not assigned a value to it..
The second row defines a function.Glfun ().This function is a function that returns char with int as the parameter. We need to understand the function at the pointer level-The function name is actually a pointer.,Function NamePointThe first address of the function code in the memory.