In-depth analysis of variable definition/declaration in C language

Source: Internet
Author: User


Even very experienced C programmers have headaches for statements that are more complex than simple arrays/pointers. For example, is the following an array of pointers or an array pointer?

Int * a [10];
What is the next item?

Int (* vtable) []) ();
Of course, this item is a pointer pointing to an array. Each element of this array is a pointer pointing to a function, and the return value type of the function is int :)

This essay hopes to teach you a very simple way to understand complex statements. I'm sure I have read this article in 99%, but I don't remember where I read it. I suspect I have discovered this myself (although I am always excited by computer language structures and mysterious things ). However, I do remember that I could write a program and convert any declaration into English.

= Golden Rule =

The rule is as follows:
Reference

Starting from the identifier (or the innermost structure, if there is no identifier, it usually appears in the function pointer), first look to the right until it is met) brackets or ends. Let's say what we see; then look to the left, until you encounter (parentheses or return to the beginning of the line, you can say what you see. Jump out of a bracket and repeat the above process: Right-click and say it; left-click and say it. Until you say the type or return value of the variable (for the function pointer), it means that you have read the declaration.

The simplest case is as follows:

Int I;
From I, you can see nothing to the right. Then you can look to the left and see the int. I is an int.

Then let's take a look at the complex:

Int * a [3];
Start from a: to the right, say "an array containing three elements"; to the left, say "each element of the array is a pointer"; to the right, nothing; to the left, indicates "pointer pointing to int ". In combination, a is an array containing three elements. Each element is a pointer pointing to an int.

Add a pair of parentheses to make it look more strange:

Int (* a) [3];
Like in normal expressions, parentheses change the reading/computing order. Starting from a: to the right, you can see parentheses and go back. To the left, you can say "it's a pointer." (parentheses, jump out.) to the right, [3]. "pointing to an array containing three elements"; to the left, int indicates "each element of the array is int ". In combination, a is a pointer pointing to an array containing three elements. Each element of the array is an int.

Okay. Let's take a look at this:

Extern int * foo ();
Like, you said: foo is a function and returns a pointer pointing to int.

Next, let's take a step: just like we can define a pointer to an int, we can also define a pointer to a function. In this case, the extern is not required (because it is not the forward declaration of the function), but the definition of a variable. This is a basic function pointer:

Int (* foo )();
Start from foo: To the right, when there is a bracket, go back; look to the left, *, say "it is a pointer", when there is a left bracket, jump out; right ,(), point to a function. To the left, int indicates that the function returns int ". In combination, foo is a pointer pointing to a function and the function returns an int.

The following is an array. Each element is a pointer pointing to a function. The Function Returns int:

Int (* Object_vtable []) ();
You still need the last incredible statement:

Int (* vtable) []) ();

And then attach some declaration examples.

Example:

Example 1

Int;
There is nothing on the right side of a. Looking at int on the left shows that a is an int variable.

Example 2

Char *;
Look to the right, there is nothing; look to the left is *, indicating that a is a pointer; then look to the right, there is nothing; then look to the left is char, indicating that a is a pointer to char.

Example 3

Int * a [];
To the right, [] indicates that a is an array; to the left, * indicates that each element of the array is a pointer; then to the right, there is nothing; then to the left, int, each pointer points to an integer. In summary, a is an array, and each element of the array is a pointer to an integer.

Example 4

Int * const;
To the right of a, there is nothing. Always look to the left, first const, indicating that a is unchangeable, then *, indicating that a is a pointer, then int, indicating that the pointer points to an integer. In summary, a is an unchangeable pointer that points to an integer.

Example 5

Void (* checkout )();
To the right, see) return, and then to the left is *, indicating that checkout is a pointer. Right, yes () indicates that the pointer points to the function; void indicates that the function returns void. In general, checkout is a pointer to the function that returns void.

Example 6

Void (* checkout []) ();
To the right, [] indicates that checkout is an array; to the left, * indicates that each element of the array is a pointer. To the right, meet) return; then to the left, meet (return. Then, you can see to the right, that is, (), indicating that each pointer in the array points to the function; void to the left, indicating that each function returns void.
In general, checkout is an array, and the array is a pointer to the function that returns void.

Example 7

Void (* checkout) []) ();
To the right, see) return; to the left, * indicates that checkout is a pointer (). To the right, [] indicates that checkout points to an array. To the left, * indicates that each element of the array is a pointer. To the right), to the left (, jump out of a layer (). To the right, () indicates that each element of the array points to a function. To the left, void indicates that the function pointed to by each array element returns void.
Conclusion: checkout is a pointer pointing to an array. Each array element is a pointer pointing to the void function returned.
This is a pointer pointing to an array. Each element of the array is a pointer pointing to a function, and the return value of the function is int. Found? This is the object_vtable pointer above, that is, the virtual function table pointer required for each object you define.

The pointer to vtable is a vtable address, for example, & Truck_vtable (a pointer to a Truck class instance virtual function table ).

= Summary =

The following example summarizes all the situations that C ++ needs to create a virtual function table for Polymorphism (like the original C Front-C ++ to C translator ).

Int * ptr_to_int;
Int * func_returning_ptr_to_int ();
Int (* ptr_to_func_returning_int )();
Int (* array_of_ptr_to_func_returning_int []) ();
Int (* ptr_to_an_array_of_ptr_to_func_returning_int) []) ();
This article is very well written. It is very suitable for people who have always forgotten the array pointer and pointer array. To sum up, that is, the left-side look around on the right will give priority to brackets.

Example:

Example 1

Int;
There is nothing on the right side of a. Looking at int on the left shows that a is an int variable.

Example 2

Char *;
Look to the right, there is nothing; look to the left is *, indicating that a is a pointer; then look to the right, there is nothing; then look to the left is char, indicating that a is a pointer to char.

Example 3

Int * a [];
To the right, [] indicates that a is an array; to the left, * indicates that each element of the array is a pointer; then to the right, there is nothing; then to the left, int, each pointer points to an integer. In summary, a is an array, and each element of the array is a pointer to an integer.

Example 4

Int * const;
To the right of a, there is nothing. Always look to the left, first const, indicating that a is unchangeable, then *, indicating that a is a pointer, then int, indicating that the pointer points to an integer. In summary, a is an unchangeable pointer that points to an integer.

Example 5

Void (* checkout )();
To the right, see) return, and then to the left is *, indicating that checkout is a pointer. Right, yes () indicates that the pointer points to the function; void indicates that the function returns void. In general, checkout is a pointer to the function that returns void.

Example 6

Void (* checkout []) ();
To the right, [] indicates that checkout is an array; to the left, * indicates that each element of the array is a pointer. To the right, meet) return; then to the left, meet (return. Then, you can see to the right, that is, (), indicating that each pointer in the array points to the function; void to the left, indicating that each function returns void.
In general, checkout is an array, and the array is a pointer to the function that returns void.

Example 7

Void (* checkout) []) ();
To the right, see) return; to the left, * indicates that checkout is a pointer (). To the right, [] indicates that checkout points to an array. To the left, * indicates that each element of the array is a pointer. To the right), to the left (, jump out of a layer (). To the right, () indicates that each element of the array points to a function. To the left, void indicates that the function pointed to by each array element returns void.
Conclusion: checkout is a pointer pointing to an array. Each array element is a pointer pointing to the void function returned.

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.