Water drop stone wearing C language Typedef problem

xiaoxiao2021-03-06  18

Basic explanation

Typedef is a keyword for the C language. The role is to define a new name for a data type. The data type here includes internal data types (INT, CHAR, etc.) and custom data types (Struct, etc.).

There are generally two in programming using TypeDef, one is a new name that is easy to remember to the variable, and the other is to simplify some complicated type declarations.

As for TypedEf, what is subtle, please follow the specific elaboration of several problems.

2. Typedef & structure problem

When defining a structure with the following code, the compiler reports an error, why? Does the C language not allowed to include their own pointers in the structure? Please guess first, then see the description:

Typedef struct tagnode {char * pitem; pnode pnext;} * pnode;

Answer and analysis:

1, the simplest use of Typedef

TYPEDEF Long Byte_4;

Give a new name for the known data type LONG, called byte_4.

2, TypedEf and structural combination

TYPEDEF STRUCT TAGMYSTRUCT {Int inum;} mystruct;

This statement actually completes two operations:

1) Define a new structure type

Struct tagmystruct {int inum; long LLEngth;

Analysis: tagmystruct is called "tag", "tag", actually a temporary name, struct keyword, and tagmystruct together, constitute this structural type, whether there is typedef, this structure exists.

We can use Struct Tagmystruct VarName to define variables, but pay attention to define variables using tagmystruct varname, because Struct and Tagmystruct are combined to represent a structural type.

2) Typedef has a name for this new structure called mystruct.

Typedef struct tagmystruct mystruct;

Therefore, MyStruct is actually equivalent to Struct Tagmystruct, we can use MyStruct VarName to define variables.

Answer and analysis

The C language certainly allows the structure to include its own pointer, we can see countless examples in the implementation of data structures such as the chain list, the fundamental problem of the above code is the application of TypeDef.

According to our above, you can know that in the process of the new structure, I encountered a statement of the PNEXT domain. The type is pnode. To know that PNODE is a new name, then the type itself has not been completed, this type The new name is still not existed, which means that the compiler does not know PNODE at all.

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;

Note: In this example, you use TypeDef to give a new name that is not fully declared. The C language compiler supports this approach.

3), standardized approach:

Struct tagnode {char * pitem; struct tagnode * pnext;}; type;

3. TypeDef & #define problem has the following two ways to define the PSTR data type, what is the difference between the two? Which one is better?

Typedef char * pstr; #define pstr char *;

Answer and analysis:

Usually, Typedef is better than #define, especially in the case of pointers. Please see example:

Typedef char * pstr1; #define pstr2 char *; PSTR1 S1, S2; PSTR2 S3, S4;

In the above variable definition, both S1, S2, S3 are defined as char *, and S4 is defined as a char, not the pointer variable we expect, and the root cause is that #define is just a simple string replacement and TypedEf It is a new name for a type.

#define usage example:

#define f (x) x * xmain () {INT A = 6, B = 2, C; C = f (a) / f (b); Printf ("% d / n", c);}

The output result of the following procedure is: 36.

For this reason, when a number of C-language programming specification is mentioned in the C-DEFINE definition, if the definition contains the expression, parentheses must be used, the above definition should be defined as follows:

#DEFINE F (X) (x * x)

Of course, if you use typedef, there is no such problem.

4. Another example of TypeDef & #define

The following code will report an error in the compiler. 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:

It is P2 error. This question once again reminded us: typedef and #define are different, it is not a simple text replacement. The Const PSTR P2 in the above code is not equal to const char * p2. Const PSTR P2 and Const long x are inherently different, both of which are read-only on variables, but the data type of the variable P2 is our own definition rather than the system inherent type. Therefore, the meaning of Const PSTR P2 is that the variable P2 defining data type as char * is read-only, so P2 errors.

(Note: There is a detailed explanation of the second article in this series) about Const.

#define and typedef

1) #define macro has a special length: you can use #ifDef, # ifndef, etc., you can use #undef to cancel the definition.

2) TypedEf also has a special length: it meets the range rules, using the typedef defined variable type to limit its scope to the defined function or file (depending on the location defined by this variable), without this feature .

5. TypeDef & Complex Variable Declaration

In programming practice, especially when seeing others code, often encounter complicated variable declarations, using typedef to simplify their own value, such as:

The following is a statement of three variables, I want to use TypDef to define an alias for them, how do I do?

> 1: INT * (* a [5]) (int, char *);> 2: void (* b [10]) (Void (*));> 3. Doube (*) () (* PA) [9]; answer and analysis:

The method of establishing a type alias for complex variables is simple, you only need to use the type name instead of the variable name in the traditional variable declaration expression, and then put the keyword typedef to the beginning of the statement.

(Note: If you feel difficult to understand the syntax of some variables, please refer to the 10th article of this series).

> 1: int * (* a [5]) (int, char *); // pfun is our type of alias type typedef int * (* pfun) (int, char *); // use a defined new type To declare object, equivalent to int * (* a [5]) (int, char *); PFUN A [5];

> 2: Void (* b [10]); // First, a new type of typedef void (* pfunparam) () is declared for the blue part of the above expression, and a new type of Types is declared. TypeDef void (pfunparam); // use the defined new type to declare the object, equivalent to VOID (* b [10]); PFUN B [10];

> 3. Doube (*) () (*) [9]; // First, a new type of typedef double (* pfun) () (//) (// overall declaration is a new type typef pfun) * pfunparam) [9]; // Use the defined new type to declare the object, equivalent to DOUBE (*) () (* Pa) [9]; PFunparam Pa;

转载请注明原文地址:https://www.9cbs.com/read-44093.html

New Post(0)