Typedef, # define, pointer constant, and constant pointer

Source: Internet
Author: User

I. Usage of typedef:


Define a type of Alias, not just a simple macro replacement. It can be used to declare multiple objects of the pointer type at the same time. Trap: typedef char * tchar;

Tchar Pa, PB; // Pa and Pb are pointer types. # Define cannot achieve this effect.

Trap: typedef char * pstr;

Int mystrcmp (const pstr, const pstr); const pstr is equivalent to char? No, it is actually equivalent to char *
Const. Const gives the entire pointer constant. To define a constant pointer, you must:

Typedef const char * cpstr;


Define platform-independent types. For example, to define a floating point type called real,

Implement typedef long double real on platform 1; implement typedef double real on platform 2;

Implement typedef float real on platform 3, that is, when cross-platform, you only need to modify typedef itself.


Define a new simple alias for complex statements. The method is: gradually replace a part of the complex declaration with an alias in the original declaration. In this loop, leave the part with the variable name to the final replacement, and the simplified version of the original declaration is obtained. For example:

Original Declaration: int * (* A [5]) (INT, char );

The variable name is A. replace a with a new alias pfun:

Typedef int * (* pfun) (INT, char); the simplest version of the original statement: pfun A [5];

Original Declaration: void (* B [10]) (void (*)());
The variable name is B. Replace pfunparam with alias 1 in the brackets on the right:
Typedef void (* pfunparam )();
Replace B with the variable on the left, and pfunx with alias 2:
Typedef void (* pfunx) (pfunparam );
The most simplified version of the original statement: pfunx B [10];

Original statement: Doube (*) (* E) [9];

The variable name is E. Replace the left part with pfuny as Alias 1:
Typedef double (* pfuny )();
Replace the variable E on the right, and pfunparamy is alias 2.
Typedef pfuny (* pfunparamy) [9];
The most simplified version of the original statement: pfunparamy E;

 

Understand the "right-left rule" available for complex statements ":
Starting from the variable name, start from the right to the left, and then turn the reading direction when it comes to a parentheses. After the analysis in the parentheses, the brackets will jump out of the brackets, or in the order of right to left, until the entire statement is analyzed. 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 element of func is a pointer. 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.

 

Examples of common usage:

Define the array type: typedef int arr [2]; arr is equivalent to int [2. Arr A; equivalent to int
A [2];

Function pointer: typedef int (* func) (void); func is equivalent to int (*) (void ). Func PF is equivalent to int
(* PF) (void );

Ii. # define: Pre-processing command. In the pre-compilation (pre-processing) stage, simple replacement is performed, and correctness check is not performed. Purpose:


Definition macro.


Header file protection character. (Essentially the same as ③)


Make some conditional judgments.

3. Constant pointer and pointer constant: const is a constant (c) pointer (z) before X, that is, const is a forward (CZ) before X ); after "*", the const is the needle constant (the first character is Z) (the first character is C), that is, the const is in reverse order after. Constant pointer ----- the address to which the object is directed can be changed, and the content stored in the address cannot be changed. Pointer constant ----- the address to which the pointer belongs is unchangeable.

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.