1. Independent syntax components declared in C Language
The Declaration tool is a very important component of C language declaration. It is the core content of all statements, Simply put: the declarative device is the identifier and any pointer, function brackets, and the following table of arrays combined with it. It is classified here for convenience.
1) pointer
(1 )*
(2) * const
(3) * volatile
(4) * const volatile
(5) * volatile const
2) direct declaration Tool
(1) identifier
(2) identifier [Table below]
(3) identifier (parameter)
(4) (declarative)
3) initialization content
(1) = initial value
The complete statement in C language includes the following:
1) type specifiers: including storage type and type delimiters
2) Declaration tool (see the above)
3) More declarers
4) Semicolon
Note: not all the preceding combinations are valid. For example, you cannot declare foo () or foo () [] like this.
2. Priority rules
The above describes the components of the C language Declaration. To understand the priority rules that must be activated in a declaration, You can intuitively reflect the rules in the following forms:
A declares to read data starting from his name and then read data in order of priority.
The priority of B is from high to low:
(1) The section enclosed in parentheses in the statement
(2) Suffix OPERATOR: brackets () indicate that this is a function, and square brackets [] indicate that this is an array.
(3) prefix OPERATOR: asterisk * indicates "pointer"
C. If the const and (or) volatile keywords follow the type specifiers (such as int and long), it acts on the type specifiers; otherwise, the asterisk (*) is the pointer next to its left.
Const char * p;/* constant pointer */
Char const * p;/* same as above */
Char * const p;/* pointer constant */
3. Examples
(1) An example of C language declaration using priority rules:
Char * const * (* next )();
The following uses priority rules to resolve this statement:
A first, let's look at the variable name "next" and notice that it is directly expanded by brackets.
B .1 so we should take the content in the brackets as a whole and conclude that "next is a pointer"
B. Consider the things outside the brackets and select between the asterisk prefix and the suffix of the brackets.
B .2 this rule tells us that the higher priority is the function brackets on the right, so the result is that "next is a function pointer pointing to a function that returns"
B .3 then, process the prefix "*" to get the content indicated by the pointer
C finally, "char * const" is interpreted as a constant pointer to a character.
The above analysis results can be summarized as follows: This Declaration indicates that next is a pointer, which points to a function. This function returns a pointer that points to a constant pointer of char type.
(2) Two examples of C language statement are analyzed:
Int * (* p []) ();
A first, let's look at the variable name "p" and notice that it is directly expanded by brackets.
B .1 so we first take the content in the brackets as a whole, and draw that "p is an array of pointers"
B. Consider the things outside the brackets and select between the asterisk prefix and the suffix of the brackets.
B .2 this rule tells us that the higher priority is the function brackets on the right, so it is concluded that "p is a pointer to the function pointer array, and each element of the array returns a function"
B .3 then, process the prefix "*" outside the brackets to get the content indicated by the pointer.
C finally interpreted "int *" as a pointer to an integer.
The above analysis results can be summarized as follows: This statement indicates that p is a pointer and points to a function pointer array. The parameter of each element in the array is null, the return type is an int pointer.
(3) analyze the C language statement in three examples:
Void (* signal (int sig, void (* func) (int );
At first glance, this is a very scary statement, but it is much simpler to analyze this statement after mastering the previous priority rules:
A first, check the variable name "signal" and notice that it is directly expanded by brackets.
B .1 so we should first take the things in parentheses as a whole, and conclude that "signal is a function pointer pointing to..., and tangible parameters return values. The return value is a pointer pointing"
B. Then consider the things outside the brackets and select between the asterisk prefix and the suffix of the brackets. Obviously, the things outside the brackets are the return values of signal.
B .2 this rule tells us that the higher priority is the function brackets on the right, so it is concluded that "the return value of signal is also a function pointer pointing"
B .3 and no "*" prefix exists.
C does not meet the requirements
The above analysis results can be summarized as follows: This Declaration indicates that signal is a pointer pointing to a function. The form parameter of this function is int and a function pointer void (* func )(), the return type is also a function pointer. The int parameter of the function, and the return value is void.
After mastering this method, you can easily deal with complicated C declaration statements.