"C language" complex type declaration

Source: Internet
Author: User
Tags modifiers readable

Original address:

http://blog.csdn.net/wangweixaut061/article/details/6549768 The original text is not allowed to reprint, but it is useful, copy a small part come over. For full text, please click on the link.

All complex pointer declarations in C are made up of a variety of declaration nesting. How to interpret complex pointer declarations? Right-left law is a well-known and commonly used method. However, the right-to-left law is not the content of the C standard, it is from the C standard of the Declaration of the method summed up. The C Standard's declaration rules are used to solve how to create a claim, and the right-hand rule is used to resolve how to identify a declaration, which can be said to be the opposite. The English text of the right-to-left law is said:

The Right-left Rule:start reading the declaration from the innermost parentheses, go right, and then go to left. When you encounter parentheses, the direction should is reversed. Once everything in the parentheses have been parsed, jump out of it. Continue till the whole declaration has been parsed.


This English translation is as follows:

Right-to-left rule: First look at the innermost parentheses, then look right and look left. Whenever you encounter parentheses, you should reverse the direction of reading. Once you have finished parsing all the parentheses inside, jump out of the parentheses. Repeat this process until the entire declaration is resolved.

I would like to make a small amendment to this rule, should be not defined by the identifier to start reading, rather than from the parentheses read, the reason is undefined identifier, because there may be multiple identifiers in a declaration, but the undefined identifier will only have one.

Now, with some examples to discuss the application of right-to-left law, let's start with the simplest, and gradually deepen:

Int (*func) (int *p);

First find the undefined identifier, is the Func, it has a pair of parentheses outside, and the left is a * number, which means that func is a pointer, and then jump out of this parenthesis, first look to the right, is also a parenthesis, which means (*func) is a function, The func is a pointer to such a function, which is a function pointer, which has a parameter of type int*, and the return value type is int.

Int (*func) (int *p, int (*f) (int*));

Func is enclosed by a pair of parentheses and has an * sign on the left, stating that Func is a pointer, jumping out of parentheses, and having a parenthesis on the right, then Func is a pointer to a function that has a formal parameter such as int * and INT (*) (int*), and the return value is of type int. Then take a look at the func parameter int (*f) (int*), similar to the previous explanation, F is also a function pointer, the function that points to has a int* type of formal parameter, the return value is int.

Int (*func[5]) (int *p);

The right side of the Func is a [] operator, stating that Func is an array with 5 elements, and the left side of Func has a *, indicating that the element of Func is a pointer, and that this is not a modifier of func, but a modification of func[5], because the [] operator has precedence over *, and Func is preceded [] Combined, so the * modifier is func[5]. Jumping out of this parenthesis, looking to the right, is also a pair of parentheses, stating that the element of the Func array is a pointer to the function type, the function it points to has a int* type parameter, and the return value type is int.


Int (* (*FUNC) [5]) (int *p);

Func is enclosed by a parenthesis, and there is another * on the left, then Func is a pointer, jumping out of parentheses, to the right is a [] operation symbol, indicating that func is a pointer to an array, and now to the left, there is an * number on the left, indicating that the element of this array is a pointer, and then a parenthesis, Indicates that the element of this array is a pointer to a function. To sum up, that is: Func is a pointer to an array whose elements are function pointers to functions that have int* parameters and return values of type int.

Int (* (*func) (int *p)) [5];

Func is a function pointer that has a formal parameter of type int*, and the return value is a pointer to an array, and the element of the array that is pointed to is an array with 5 int elements.

It is important to note that some complex pointer declarations are illegal, such as:

int func (void) [5];

Func is a function that returns a value that has an array of 5 int elements. However, the function return value of the C language cannot be an array, because if the function returns the value array, then the object that receives the contents of the array must also be a valarray, but the C-language array name is an rvalue, and it cannot be an lvalue to receive another array, so the function return value cannot be array.

int func[5] (void);

Func is an array of 5 elements, and the elements of this array are functions. This is also illegal, because the elements of an array must be the same in addition to the type of memory that each element occupies, and obviously the function cannot achieve this requirement, even if the function is of the same type, but the space occupied by the function is usually not the same.

As an exercise, here are a few complex pointers to the reader's own interpretation, and the answer is in the tenth chapter.

Int (* (*FUNC) [5][6]) [7][8];

Int (* (* (*FUNC) (int *)) [5]) (int *);

Int (* (*func[7][8][9]) (int*)) [5];

In practice, when a complex pointer needs to be declared, it is a major detriment to the readability of the program if the entire declaration is written in the form shown above. A typedef should be used to decompose the declaration layer by level to enhance readability, for example, for declarations:

Int (* (*func) (int *p)) [5];

Can be decomposed like this:

typedef int (*para) [5];
typedef PARA (*FUNC) (int *);

This makes it easier to see.

C Statement of complex types of speech

In the complex type declaration of C speech, we use 3 modifiers: *, (), []. The meaning is as follows:
* Implied declaration of a pointer
() implies declaring a function
[] implies declaring an array
The C language allows us to declare a variable at once using multiple modifiers described above, so that we can construct a variety of categories.
Let's look at the following example:
int board[8][8]; Two-dimensional array (an array of 8 elements, element contents, or arrays)
int **p; Pointer to pointer (a pointer, content, or a pointer)
int *array[10]; An array of 10 elements, the element content is a pointer.
int (*p) [10]; A pointer to an array that contains 10 elements.
int *oof[3][4]; An array of 3 elements, the contents of which are pointers, and pointers to arrays of 4 elements, respectively
int (*oof) [3][4]; An array of pointers pointing to 3*4.

See here, maybe someone will think, what you are saying oh, how do not understand, no relationship, read the following 3 rules, you are looking at the above content is very clear.
1: The higher the modifier precedence from the name
2:[], () priority is higher than *
3: Expressions that are enclosed in () have the highest precedence.

Extract something from http://jingyan.baidu.com/article/22a299b50dd8e59e18376a77.html. About Const is very useful.

Let me summarize:

    • The const int * P:p is a pointer to an int, which is not writable.
    • int * Const P:P is not writable, p is a pointer to int
    • The int const * P:P is a pointer to the content that is not writable and whose contents are of type int.

Here is the original:

Continue to add: Add Const. The above understanding of complex declarations, discerning eye know only from the perspective of the right value. To understand a const, you must have to mention the left value. Lvalue Rvalue is the basic concept in C + + and few words is not clear. The most superficial view, lvalue means can be written, that is, can appear on the left side of the assignment statement, the right value is readable, only on the right of the assignment expression. Of course, in general, the lvalue can often be used as the right value, and the writable is often meant to be readable. and the function of the const, is to be able to write things, to the whole into a read-only . In terms of expression, the value of some expressions is left and right, and const removes its lvalue function . Let's say, for example, from the simplest of words.

int A; The result of expression A is int, which is both Lvalue and rvalue;

const INT
The result returned by a;,a is of type int, but the result is no longer writable, that is, a cannot be placed on the left side of an assignment statement.

int const A; It is understandable that you should ignore int
Const,a is a variable that is both readable and writable, and const converts a to read-only. int represents the result type of const a. Although, in this sense, for the compiler, the const int
A; and int const A; all the same, all just express the same meaning, A is an integral type constant.

const INT
*p;,*p result is int type, plus const, *P can only read, so, p is the shaping pointer, the content of which can only be read and not written, but p itself can be written. int const
*p, the *p is first emphasized, and then the *p is the int type. In fact, both of these formulations are the same.

The int *const P;,const is immediately next to P, stating that p itself is read-only. As for int
*, indicates that the *p type is int and can be read and written. Therefore, p is the shaping pointer, and p itself is not writable, but the content it refers to is readable and writable. To be honest, I do not understand what the ghost of this pointer variable is, and the actual code should be used very rarely.

In order to express the concept of read-only purity of the pointer, not only the pointer itself can not write, and even its point to the content can not be modified, C + + finally complete the following such a great code. Int
const *CONST p; or const int * const
P;. This practice of C + +, commonly known as working to solve imaginary problems, because the combination of const and pointers, in fact, only the pointer to the content of the point of reading is very meaningful, and the other, the meaning is negligible.

"C language" complex type declaration

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.