Original address: http://blog.csdn.net/czmpersist/article/details/9288205
The last round tells the pointer memory and address, finally mentions the type description, this round continues the pointer type description. Type description
To understand the pointer, more or less will appear some more complex types, today first introduce how to fully understand a complex type, to understand that complex types are regular can be found, after finding the rule everything becomes very simple, a type will appear a lot of operators, they also like ordinary expressions, have priority, The priority is the same as the operation priority, so I summarize the principle:
From the variable name, according to the operator priority combination, step-by-step analysis.
The following I will use several operators to express, for your reference, from top to bottom priority is gradually reduced.
Associativity and Operator Operators and their associativity |
Function Function |
Use Usage |
L |
[] |
Subscript (subscript) |
variable [expr] |
L |
() |
function call (functions called) |
Name (expr_list) |
R |
* |
Dereference (dereference) |
*expr |
Let's start with a simple type of analysis:
Ø int p;
This is a generic integer variable.
Ø int *p;
First from P, first with *, so that P is a pointer, and then combined with int, indicating that the pointer to the content of the type int, so P is a pointer to return the integer data .
Ø int p[3];
First, starting at p, first with [], that P is an array, and then combined with int, that the elements in the array are integral, so P is an array of integer data .
Ø int *p[3];
First, start with the [], because [] precedence is higher than *, so p is an array, and then combined with the *, the element in the array is a pointer type, and then combined with an int, indicating that the pointer is the type of the content pointed to the integer, so P is an array of pointers that return an integral type of data .
Ø Int (*p) [3];
First, starting from P, first with the *, that P is a pointer and then the [] combination (and "()" This step can be ignored, just to change the priority), indicating that the pointer to the content is an array, and then combined with int, the elements in the array is an integer type. So P is a pointer to an array that consists of integral types of data .
Ø int **p;
First, starting with the P, the first with the right (* Union direction from right to left), said P is a pointer, and then combined with the *, indicating that the pointer is pointing to the element is a pointer, and then combined with an int, indicating that the pointer to the element is an integer data. Because two-level pointers and more advanced pointers are rarely used in complex types, we don't consider multi-level pointers for more complex types later, with a maximum of one-step pointers being considered.
Ø int p (int);
From P, first with (), that P is a function, and then into () analysis, indicating that the function has an integer variable parameters and then with the outer int, indicating that the function return value is an integer data.
Ø Int (*p) (int);
Starting at p, first with the pointer, stating that P is a pointer, and then combined with (), indicating that the pointer is pointing to a function, and then in the () with the int, that the function has an int parameter, and then the outermost int, the return type of the function is integer, so P is a pointer to a function that has an integer parameter and returns a type of integer .
Ø int * (*p (int)) [3];
Starting with the P, first with (), the P is a function, and then enter (), and int, the function has an integer variable parameter, and then with the outside of the *, the function returns a pointer, and then to the outermost layer, first with [], indicating that the returned pointer to an array, And then with the *, the element in the array is a pointer, and then combined with an int, indicating that the pointer to the content is the integer type data. So P is a function whose parameter is an integer data and returns a pointer variable that points to an array of integer pointer variables .
These are the reference to the rectification version of the C language, as well as the information found on the Internet to synthesize the results, to help you understand the pointer type. If the above so the situation can understand, for the pointer definition is similar, too many too complex definitions in the actual development process is difficult to understand, less readable, the application will not be too broad, look at the reader's own measurement. Note: All of the pointers in the previous definitions are not initialized, which means we don't know where the pointers are pointing, which is something that programmers must be aware of.
The above is only for the type of int, for char,double, structure and other types of principle, we hope you know a counter three. OK, today is about here, step by step, do not worry, when you grasp the basis of the good can change, ingenious, or the same, if you have some suggestions, or did not involve, to everyone pointing, younger brother will be actively adopted!
[Go] Interpreting C-pointers (2)--type description