Interpret the clockwise/spiral rule (clockwise spiral rules) __c++ of C + + declarations

Source: Internet
Author: User
Tags volatile
The ' clockwise/spiral rule ' by David Anderson

There is a technique known as the "clockwise/spiral rule" which enables any C programmer to parse into their head any C de claration!

There are three simple steps to follow:starting and the unknown element, move in a spiral/clockwise direction; When ecountering the "following elements replace them with the corresponding 中文版 statements:

[x] or [] => Array X size of ... or array undefined size of ... (Type1, type2) => function passing type1 and type2 returning ... * => pointer (s) to ...

Keep doing a spiral/clockwise direction until all tokens have been.

Always resolve anything in parenthesis first! Example #1: Simple declaration

                     +-------+
                     | +-+   |
                     | ^ |   |
                Char *str[10];
                 ^   ^   |   |
                 |   +---+   |
                 +-----------+
Question we ask Ourselves:what is STR? ' Str is ' ... We move in a spiral clockwise direction starting with ' str ' and the ' the ' of the ' the ' and ' the ' we are a ' [' So, that character we means An array, so ... ' str ' is ' an array ' ... Continue in a spiral clockwise direction, and the next thing we encounter are the ' * ' so, which means we have pointers, so. . ' Str is ' a array of pointers to ... Continue in a spiral direction and we are the end of the "the", so keep going and we have to the type ' char '. . ' Str is ' a array of pointers to Char ' We have now ' visited ' every token; Therefore we are done! Example #2: Pointer to Function declaration
 +--------------------+ | +---+ |              | |+-+|
                     |              | |^ ||
                |
                 char * (*FP) (int, float *);              ^   ^ ^  ||
                 |   | |              +--+|
                 |   |
                 +-----+              | +------------------------+
Question we ask Ourselves:what is FP? ' FP is a ... Moving in a spiral clockwise direction, the ' a ' thing we are ' a ') '; Therefore, FP is inside parenthesis, so we continue the spiral inside the parenthesis and the next character is the ' * ', so ... ' FP is a pointer to ... We are now out of the "parenthesis" and continuing in a spiral clockwise direction, we have the ' ('; therefore, we have a fun Ction, so ... ' FP is a pointer to a function passing a int and a pointer to float returning ... Continuing in a spiral fashion, we then you the ' * ' character, so ... ' FP is a pointer to a function passing a int and a Pointer to float returning a pointer ... Continuing in a spiral fashion we are the '; ', but we haven ' t visited all tokens, so we continue and finally get to the Ty PE ' char ', so ... ' FP is a pointer to a function passing an int and a pointer to float returning a pointer to a char ' Example #3: The ' ultimate '
                      +-----------------------------+
                      |                  +---+      |
                      |  +---+           |+-+|      |
                      |  ^   |           | ^ ||      |
                void (*signal (int, void (*FP) (int))) (int);
                 ^    ^      |      ^    ^  ||      |
                 |    +------+      |    +--+|      |
                 |                  +--------+      |
                 +----------------------------------+

Question we ask ourselves:what is ' signal '?

Notice that signal is inside parenthesis and so we must resolve this first! Moving in A clockwise direction we are (' so we have ... ' signal is a function passing an int and a ... Hmmm, we can use this same the rule in ' FP ', so ... What is FP? FP is also inside parenthesis so continuing we are ' * ', so ... FP is a pointer to ... Continue in a spiral clockwise direction and we have to ' (', so ... ' FP is a pointer to a function passing int returning.. .'' Now we are continue out of the function parenthesis and we are void, so ... ' FP is a pointer to a function passing int returni ng nothing (void) "We have finished with FP so let's catch up with ' signal '", We now have ... ' signal is a function passin G an int and a pointer to a function passing an int returning nothing (void) returning ... We are still inside parenthesis so the next character seen be a ' * ', so ... ' signal is a function passing a int and a POI Nter to a function passing a int returning nothing (void) returning a pointEr to ... We have now resolved the items within parenthesis, so continuing clockwise, we then to the another ' (', so ... ' signal is a f Unction passing an int and a pointer to a function passing an int returning nothing (void) Returning a pointer to a FUNCTI On passing an int returning ... Finally we continue and the only thing left is the word ' void ', so the final complete definition for signal is: ' SIG NAL is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer t o a function passing an int returning nothing (void) '

The same is applied for const and volatile. For Example:

	const char *chptr;
Now, what is Chptr?? ' Chptr is a pointer to a char constant '

How about this one:

	char * const CHPTR;
Now, what is Chptr?? ' Chptr is a constant pointer to char '

Finally:

	Volatile char * const CHPTR;
Now, what is Chptr?? ' Chptr is a constant pointer to a char volatile. '

Practice this and the examples found in K&r II on page 122.


copyright©1993,1994 David Anderson

This article may is freely distributed as long as the author ' name and this notice are.

Related Article

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.