Lu Qichen
http://blog.9cbs.net/happydeer/archive/2004/07/13/40219.aspx original: http: //www.codeproject.com/cpp/complex_declarations.asp Author: Vikram A Punathambekar introduced once caught Let you confuse you, similar to int * (* (* (* fp1) (int)) [10];
of
Variable declaration? This article will be easy to be difficult, step by step, how do you understand this?
Aprotic
Miscellaneous C / C declaration: We will have a simple statement you can encounter every day
Start with
Then gradually add the const modifier and TypeDef, and function pointer,
Final introduction
A "right left method that allows you to accurately understand any C / C declaration
"Needed.
Emphasize that complex C / C declarations are not good programming
Style; I am here
Just teach you how to understand these statements. Note: In order to protect
Certificate can be in the same
Show code and related comments on the line, this article is preferably at least at least
1024x768 resolution
Read it on the instrument. The basis allows us to start from a very simple example, as follows: int N; this should be understood as "declare n as an int" (n is an INT type variable). Take a look at the pointer variables, as follows: int * p; this should be understood as "declare p as an int *" (P is an int * change
Quantity), or P is a pointer to an INT type variable. I want to show here
Open discussion: I think when declaring a pointer (or reference) type variable,
It is best to write * (or &) before the variable is obedient, not followed by the basic type.
This will avoid some misunderstandings, such as int * p, q; first look, it seems to be p and q are int * type, but in fact, only P is
A pointer, and q is the simplest INT variable. We still continue our topic, then look at the example of a pointer: char ** argv; theoretically, there is no restriction on the number of pointers, you can define a floating point type
The pointer of the pointer of the pointer of the variable, the pointer of the pointer of the pointer ... Int rollnum [30] [4]; int (* p) [4] = rollnum; int * q [5]; here, P is declared For a pointer pointing to a 4-element (int type) array,
And Q is declared as an array containing 5 elements (INT type pointers). In addition, we can also mix practical * and &, as follows: int ** p1; // p1 is a pointer to a pointer to an origency to a pointer to An int.int & * p3; // error: Pointer to a reference is illegal.int && p4; // error: reference to a reference is illegal. Note: P1 is a pointer for an int type pointer; P2 is an int type Pointer
Quote; P3 is an int type reference pointer (not legal!); P4 is an int
Type references (not legal!). Const modifications When you want to block a variable, it may be used to use the const key. While you give a variable plus Const modifiers, it usually needs to initialize it.
Because you will have no chance to change it anymore. For example: const INT n = 5; int const m = 10; the above two variables n and m are actually the same type - all const Int
In constant amounts). Because the C standard specifies, the const key is placed in the type or variable name.
Before equivalent. I personally prefer the first statement, because it highlights
Const modifier effect. It is easy for people to be confused when Const is working with a pointer. For example, let's come
Look at the declaration of the following P and Q: const INT * p; int const * q; which one represents the const Int type pointer (Const Directly Modified Int),
Which one represents an int type const pointer (Const direct finishing pointer)? actual
On, p and q are declared as a CONST INT type pointer. INT type constant
The needle should be declared: int * const r = & n; // n HAS been declared as an INT This, P and Q are pointers to the Const int type, that is, you are later
The value of * p cannot be changed in the program. And R is a const pointer, when it is declared
After being initialized to point to the variable N (ie R = & n;), the value of R will no longer be changed.
Change (but the value of * r can be changed). Combine the above two constrains, let's declare a point to const Int
The type of Const pointer, as follows: const * const p = & n// n Has Been Declared As Const Int The following is given about constrails, will help you completely clarify const
usage. However, please note that some of the statements below cannot be compiled, because
To initialize while the statement is required. For the sake of simplicity, I ignore
Initialization section; because of the initialization code, each of the following declarations will
Add two lines of code. Char ** p1; // Pointer to P2; // Pointer to POINTER TO Const Charchar * const * p3; // Pointer to const p4; // Pointer to const P4; // Pointer To Const Pointer To const charchar ** const p5; // const power to pointer to charconst char ** const p6; // const Pointer to POINTER TO Const Charchar * const * const p7; // const Pointer to const Pointer to Charconst Char * Const * Const P8; // Const Pointer to const POINTER TO Const Char Note: P1 is a pointer to a pointer to a CHAR type; P2 is a pointer to the CONST CHAR type pointer; P3 is a const pointer to a CHAR type; P4 is pointing
Const char type Const pointer; p5 is a const refer to a pointer to a CHAR type
Needle; P6 is a Const pointer to the const char type pointer; P7 is directed to char
CONST pointer for type Const pointer; P8 is a const char type of Const CONST
Needle of Const pointers. Typedef's wonderful use of TypeDef to overcome "* only suitable for variables and not suitable for type"
The drawbacks. You can use typedef: typedef char * pchar; pchar p, q; the P and Q here are declared as pointers. (If you don't use TypeDef, Q will be acoustic
Ming is a char variable, which is not consistent with our first eye! )the following
There are some declarations that use TypeDef and give the explanation: typedef char * a; // a is a pointer to a chartypedef a b (); // b is a function truRNS // a Pointer to a chartypedef b * c ; // c is a pointer to a function // That Returns a Pointe to a chartypedef c d (); // d is a function Returning // a Pointe to a function // That Returns a Pointer to a chartypedef d * e ; // e is a pointer to a function // Returning a Pointer to a // function That Returns a // pointer to a chare var [10]; // var is an array of 10 Pointers to // functions return Pointers To // functions return points to chars.typedef is often used before a structural declaration, as follows. In this way, when the structural variable is created, you will not use the keyword structure (in C, create a structure)
When the amount is required to use the struct key, such as Struct TagPoint A; in C ,
Struct can ignore, such as tagpoint b). Typedef struct tagpoint {int x; int y;} Point; Point P; / * Valid c code * / Function Pointer function pointer may be the most prone to understanding declaration. Function pointer
The TSR program is used up to the TSR program in the DOS time; in the Win32 and X-Windows era,
They are used in the case where the callback function is needed. Of course, there are many other places need
To use a function pointer: Some templates in STL, WIN NT / 2K / XP
System service, etc. Let's take a simple example of a function pointer: int (* p); here P is declared as a function pointer, this function with a char type
Number, and there is an int type return value. In addition, there are two float types
Number, the return value is the function pointer of the pointer of the CHAR type pointer can be declared as follows: char ** (* p) (float, float); then with two CHAR type Const pointer parameters, no reference to the value of the value
What should I declare? Reference is as follows: void * (* a [5]); "right left law" [important! ! ! ] The Right-Left Rule: Start Reading The Declaration from the innermost
ParentheSes,
Go right, and then go left. When you encounter
Parenthese, The Direction SHOULD BEENTHESES HAS BEEN PARSED, JUMP OUT OF IT. Continue Till T10
Whole Declaration Has Been Parsed. This is a simple rule, but you can make you accurately understand all the statements. This method
Then use the following: From the most internal brackets, read the declaration, look at it right, then
Left. When you encounter a parentheses, you will turn the direction of reading. All in parentheses
The content is analyzed and jumped out of the scope of parentheses. This continues until the entire statement
They were all analyzed. Do a small correction for the above "right left law": when you first start reading
When you declare, you must start from the variable name, not from the most in-room bracket. The following combine example to demonstrate the use of "right left default". INT * (* (* fp1) (int)) [10]; reading steps: 1. Starting from the variable name ----------------------- ------------------ fp12. Go to the right, nothing, touched it), so see it, come to a * ------ a pointer 3. Jump out of parentheses and meet (int) --------------------------------- A with an int parameter Function 4. Look left, find one * ------------------------- (Function) Returns a pointer 5. Jump out of parentheses, look at it right, encounter [10] ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- An array of 10 elements 6. Look left, find a * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- Pointer 7. Look left, discovery int ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- int I summary: FP1 is declared as an array of pointers of a function of a function, this array
There are 10 elements, the prototype of the function is a parameter with an int type, and the return value is one.
Pointer? Let's take another example: int * (* arr [5]) ()) (); reading step: 1. Starting from the variable name ----------------- ------------------------ ARR2. Tour right, found is an array --------------- ------------------- A 5-element array 3. Look left, find a * ----------------- ---------------------- Pointer 4. Jump out of parentheses, look at it, discovery () --------------- ----------------- Do not with parameters 5. Look left, touch * ------------------- ------------------------ (function) Returns a pointer 6. Jump out of parentheses, discover () to right () ---------- -------------------------- Do not use the parameters 7. Left, discovery * ------------ -------------------------------- (function) Returns a pointer 8. Continue to the left, discover Int --- ----------------------------------- int ration summary:? ? There are more examples: float (* (* b ()) []) (); // b is a function That Returns a // pointer to functions returnid floats.void * (* c) (char, int (*)); // c is a pointer to a function this takes // two parameters: // a char and a pointer to a // function Takes no // parameters and return / / An int // and returns a pointer to void.void ** (* d) (int &, char ** (*) (char *, char **));