Clockwise/spiral rules to understand C + + complex definitions

Source: Internet
Author: User

This article is translated from Spiral rule and is attached to the full text.

Clockwise/Spiral Rule

The clockwise/Spiral rule is a way for any C programmer to understand the program's claims.
The following 3 steps:
1. Starting with the element to determine the type, replace the following element with the corresponding statement in a clockwise direction, for example:
[X] or []
==>array X size of ... or Array undefined size of.;
(type1,type2)
==>function passing type1 and type2 returning ...
*
==>pointer (s) to ...

2. The encountered element has been replaced in this manner until all elements have been replaced.
3. The contents of the parentheses are always processed first.

Chestnuts
#1: Simple declaration
+-------+
| +-+ |
| ^ | |
Char *str[10];
^ ^ | |
| +---+ |
+-----------+
Ask yourself first: what is STR?
"Str is
A. We start with ' str ', and in a clockwise direction, the first sign encountered is ' [', which indicates that this is an array, so:
' Str ' is an array containing 10 XX (xx is not currently determined)
B. Continue to move in a clockwise direction, the next encounter is ' * ', indicating that this is a pointer, so:
' Str ' is an array containing 10 x pointers;
C. Continue to move in a clockwise direction, and now encounters the '; ' of the end of the line, so it continues to move and encounters the type ' char ', so:
' Str ' is an array containing 10 char pointers;

#2: function Pointer declaration
+--------------------+
| +---+ |
| |+-+| |
| |^ || |
char * (*FP) (int, float *);
^ ^ ^ || |
| | +--+| |
| +-----+ |
+------------------------+
First ask yourself: what is FP?
"FP is a"
A. Move clockwise, the first element we encounter is ') ', so the FP continues to move (within the parentheses range, Rule NO 3).
The next symbol encountered is ' * ', so:
FP is a pointer to XX;
B. Jump out of parentheses and continue to move in a clockwise direction, encountering ' (', so this is a function, so:
The FP is a pointer to a function that returns XX (the parameter of the function is an int and float pointer);
C. Continue to move clockwise, encounter the ' * ' symbol, so:
The FP is a pointer to a function that returns an XX pointer;
D. Continue to move clockwise, encounter '; ' But we have not yet accessed all the elements, continue to move encounters type ' char ', so:
The FP is a pointer to a function that returns a char pointer;

#3: "Ultimate"
+-----------------------------+
| +---+ |
| +---+ |+-+| |
| ^ | |^ || |
void (*signal (int, void (*FP) (int)))) (int);
^ ^ | ^ ^ || |
| +------+ | +--+| |
| +--------+ |
+----------------------------------+

Question: What is ' signal '?
Note that ' signal ' is within the brackets, so priority should be given;
A. Move in a clockwise direction, encounter ' (', so:
' Signal ' is a function (the arguments passed are int and one?). )
B. Haha, we can use the same rules to deal with ' FP ', the same question: ' FP ' what is it? Since ' FP ' is also within a parenthesis, move on to see ' * ',
So:
' FP ' is a pointer;
B1. Continue to move clockwise, encounter ' (', so:
' FP ' is a pointer to a function (the function argument is an int, which returns XX);
B2. Continue to move outside the brackets and encounter ' void ';
' FP ' is a pointer to a function (the function argument is an int, and the return is null);
' C.FP's resolution is over, we continue to see ' signal ', so far we know:
' Signal ' is a function (the argument is an int and a pointer to a function (the function argument is an int, and the return is null);
D. We are still in parentheses, so the next sign we encounter is ' * ', so:
' Signal ' is a function (the argument is an int and a pointer to a function (the function argument is an int, and the return is null) to return a pointer to XX;
E. Now that we have solved all the elements in parentheses, we continue to move clockwise, and we encounter another ' (', so:
' Signal ' is a function (the argument is an int and a pointer to a function (the function argument is an int, which returns null), and a pointer to the function (the function with the parameter int, which returns XX);
F. Finally, move on to the remaining symbol ' void ', so the final definition is:
' Signal ' is a function (the argument is an int and a pointer to a function (the function argument is an int, which returns null), and a pointer to the function (which is an int, which returns an empty function);

This rule also applies to const and Volatile (Http://en.wikipedia.org/wiki/Volatile_ (computer_programming)).
For example:
const char *chptr;
A. What is ' chptr '?
' Chptr ' is a pointer to a char constant;
This chestnut:
char * const CHPTR;
What is A.chptr?
' Chptr ' is a constant pointer to a char type;

The last chestnuts:
Volatile char * const CHPTR;
A. What is ' chptr '?
Chptr is a constant pointer to ' char volatile '

Original
[This is posted to comp.lang.c by it author, David Anderson, on 1994-05-06.]
The ' clockwise/spiral Rule '

by David Anderson

There is a technique known as the "Clockwise/spiral Rule" which enables any C programmer to parse in their head any C de claration!

There is three simple steps to follow:

Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements Replac e 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 this in a spiral/clockwise direction until all tokens has been covered.
always resolve anything in parenthesis first!
Example #1: Simple declaration

+-------+
| +-+ |
| ^ | |
Char *str[10];
^ ^ | |
| +---+ |
+-----------+
Question we ask Ourselves:what is STR?
"Str is a ...
We move in a spiral clockwise direction starting with ' str ' and the first character we see are a ' [' so, which means we have An array, so ...
"STR is an array of ...
Continue in a spiral clockwise direction, and the next thing we encounter are the ' * so, that means we had pointers, so. .
' STR is an array of pointers to ...
Continue in a spiral direction and we see the end of the line (the "; '), so keep going and we get to the type ' char ', so. .
' STR is a array of pointers to char '
We have now ' visited ' every tokens; 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 first thing we see is a ') '; Therefore, FP is inside parenthesis, so we continue the spiral inside the parenthesis and the next character seen are the ' * ', so ...
"FP is a pointer ...
We are now out of the parenthesis and continuing in a spiral clockwise direction, we see the ' ('; therefore, we had a fun Ction, so ...
' FP is a pointer to a function passing an int and a pointer to float returning ...
Continuing in a spiral fashion, we and see the ' * ' character ...
' FP is a pointer to a function passing an int and a pointer to float returning a pointer to ...
Continuing in a spiral fashion we see 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 are inside parenthesis, so we must resolve this first!

Moving in a clockwise direction we see ' (' So we have ...
' Signal is a function passing an int and a ...
Hmmm, we can use the this same rule on the ' FP ', so ... What is FP? FP is also inside parenthesis so continuing we see a ' * ', so ...
FP-A pointer to ...
Continue in a spiral clockwise direction and we get to ' (', so ...
' FP is a pointer to a function passing int returning ... '
Now we continue out of the function parenthesis and we see void, so ...
' FP is a pointer to a function passing int returning nothing (void) '
We have the finished with the FP so let's catch up with ' signal ' and we have ...
' Signal is a function passing 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 was a ' * ', so ...
' Signal is a function passing an int. and a pointer to a function passing an int returning nothing (void) returning a poin ter to ...
We have now resolved the items within parenthesis, so continuing clockwise, we then see another ' (', so ...
' Signal is a function passing an int. and a pointer to a function passing an int returning nothing (void) returning a poin ter to a function passing an int returning ...
Finally We continue and the only thing are the word ' void ', so the final complete definition for signal is:
' Signal is a function passing an int. and a pointer to a function passing an int returning nothing (void) returning a poin ter to a function passing an int returning nothing (void) '
The same rule is applied for Const and volatile. For Example:

const char *chptr;
Now, who is Chptr??
' Chptr is a pointer to a char constant '
How on this one:

char * const CHPTR;
Now, who is Chptr??
"Chptr is a constant pointer to char"
Finally:

Volatile char * const CHPTR;
Now, who is Chptr??
"Chptr is a constant pointer to a char volatile."
Practice this rule with the examples found in K&r II on page 122.

copyright©1993,1994 David Anderson
This article is freely distributed as long as the author ' s name and this notice is retained.

Clockwise/spiral rules to understand C + + complex definitions

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.