Clockwise/Spiral Rule in C/C++ with Examples
Last Updated :
22 Sep, 2021
The Spiral/Clockwise Method is a magic tool for C/C++ programmers to define the meaning of syntax declaration in the head within seconds. This method was created by David Anderson and here is a short brief about how to apply this method.
While coding, when someone comes across a new unwanted syntax declaration:
string(const** f(int, void (*const p)(int)))(char[]);
The question here arises is what is the meaning of the function name “f”? There is an infamous way that will help one to deduce the meaning of this function “f” in seconds.
Approach:
Step 1: Consider this Clockwise-spiral.

Step 2: Replace O with a.

Step3: Now, along the clockwise path, replace “|” with some words and try to get the meaning of syntax, and replace “|” with “(“. The round brackets explain its function and say “‘a’ is a function”.

Step 4: Consider seeing what is inside of the round brackets/function, so the open brackets (remember like from precedence of operators, brackets are always opened first) and go from left to right until the end of round brackets “)” are encountered. It can be said that “‘a’ is a function with the integer ‘x’ as an argument”.

Step 5: Now come back to the first round bracket and continue the spiral path from “(“. Replace “|” with “*”. This means a return value and pointer. Here “‘a’ is a function with the integer ‘x’ as an argument which returns a pointer to “.

Step 6: Continue the spiral clockwise. One will encounter “int” which was already dealt with, so continue the spiral path.

Step 7: Replace “|” with “int”. It can be said that “‘a’ is a function with the integer ‘x’ as an argument which returns a pointer to an integer”.

Step 8: Again, continue this path and one will get “;” which means this is the end of the statement, and see that’s how the meaning of syntax can be defined easily.

Finally, it can be said that “‘a’ is a function with an integer ‘x’ as an argument which returns a pointer to the integer”.
Answer: f is a function with arguments as int and p a function with the argument as int, returning a constant pointer to nothing (void) returning a pointer to a pointer to a constant function with the character array as an argument returning a string.
Important Points:
- Syntax Explanations:
- A[ ]: array A is of undefined size and A[ 5 ] means the size of array is 5.
- (type 1,type 2): function passing type1 and type2.
- *: pointer to.
- Start with the variable name.
- Keep this reading until all modifiers in the variable declaration are overwritten.
- Always complete any content in parentheses first.
- Keep doing this in a spiral/clockwise direction until all tokens have been covered.
Example:
Consider an example by visualizing a spiral in the brain:
(char const *p) ( int , const (int *)*);
Remember the thumb rule. Always start with the name of a variable. Starting from a variable p clockwise spiral.
Step 1: ‘p’ is the variable.
p
Step 2: ‘p’ is a variable that is a pointer to.
p -> *
Step 3: ‘p’ is a variable that is a pointer to a function.
p -> * -> (
Step 4: ‘p’ is a variable that is a pointer to a function with int as an argument.
p -> * -> ( -> int
Step 5: For the next step, there is no variable name, so give one. Let it be ‘a’. So the expression can be written as:
(char const *p) ( int , const (int *)* a );
Step 6: Here ‘a’ is a variable.
a
Step 7: Closed round bracket doesn’t tell anything, so continue.
a-> )
Step 8: In the next step, a is a variable pointer to.
a -> ) -> *
Step 9: Ignore the semicolon because the syntax definition is not complete, so continue.
a -> ) -> * -> ;
Step 10: The closed round bracket doesn’t tell anything, so continue.
a -> ) -> * -> ; -> )
Step 11: Now ‘a’ is a variable pointer to pointer.
a -> ) -> * -> ; -> ) -> *
Step 12: ‘a’ is a variable pointer to pointer int.
a -> ) -> * -> ; -> ) -> * -> int
Step 13: ‘a’ is a variable pointer to pointer int constant.
a -> ) -> * -> ; -> ) -> * -> int -> const
Let’s get back to where defining ‘p’ was left.
Step 14: ‘p’ is a variable that is a pointer to a function with int as an argument and a variable pointer to the pointer int constant which returns a constant.
p -> * -> ( -> int , const... -> const
Note:
The reused terms already used for defining syntax are being ignored here.
Step 15: p is a variable that is a pointer to a function with int as argument and a variable pointer to pointer int constant which returns a constant character.
p -> * -> ( -> int , const... -> const->char
This finishes the complete meaning of the syntax.
Note:
There is an exception to this rule when syntax has arrays (especially multidimensional arrays), so there is an update to this rule.
- When an array is encountered, move to the most-right closing square bracket and treat the array as one ‘sweep’ “
- Example:
- int *a[10][]: ‘a’ is a multidimensional array of size 10 and an undefined size of pointers to int.
Similar Reads
Pi(π) in C++ with Examples
In this article, we will discuss some of the mathematical function which is used to derive the value of Pi(Ï) in C++. Method 1: Using acos() function: Approach: The value of Î is calculated using acos() function which returns a numeric value between [-Î , Î ].Since using acos(0.0) will return the valu
2 min read
sin() function for complex number in C++ with Examples
The sin() function for complex numbers is defined in the <complex> header file. This function is the complex version of the sin() function. This function is used to calculate the complex sine of the complex number z. This function returns the sine of the complex number z. Syntaxsin (z); Parame
2 min read
<complex.h> header file in C with Examples
Most of the C Programs deals with complex number operations and manipulations by using complex.h header file. This header file was added in C99 Standard. C++ standard library has a header, which implements complex numbers as a template class, complex<T>, which is different from <complex.h
5 min read
How to find arctangent with Examples
What is arc tangent?The arctangent is the inverse of the tangent function. It returns the angle whose tangent is the given number. catan() is an inbuilt function in <complex.h> header file which returns the complex inverse tangent (or arc tangent) of any constant, which divides the imaginary a
15+ min read
Reversed Range-based for loop in C++ with Examples
Range-based for loops is an upgraded version of for loops. It is quite similar to for loops which is use in Python. Range-based for loop in C++ is added since C++ 11. We can reverse the process of iterating the loop by using boost::adaptors::reverse() function which is included in boost library Head
2 min read
Clockwise Spiral Traversal of Binary Tree | Set - 2
Given a Binary Tree. The task is to print the circular clockwise spiral order traversal of the given binary tree.Examples: Input : 1 / \ 2 3 / \ \ 4 5 6 / / \ 7 8 9 Output :1 9 8 7 2 3 6 5 4 Input : 20 / \ 8 22 / \ / \ 5 3 4 25 / \ 10 14 Output :20 14 10 8 22 25 4 3 5 We have already discussed Clock
11 min read
C++ Program for Clockwise rotation of Linked List
Given a singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, K = 2 Output: 4 -> 5 -> 1 -> 2 -> 3 -> NULLInput: 7 -> 9 -> 11 -> 13 -> 3 -> 5 -> NULL
4 min read
Reverse Anti Clockwise Spiral Traversal of a Binary Tree
Given a binary tree, the task is to print the nodes of the tree in a reverse anti-clockwise spiral manner. Examples: Input : 1 / \ 2 3 / \ \ 4 5 6 / / \ 7 8 9 Output : 7 8 9 1 4 5 6 3 2 Input : 20 / \ 8 22 / \ / \ 5 3 4 25 / \ 10 14 Output : 10 14 20 5 3 4 25 22 8 Approach: The idea is to use two va
11 min read
Clockwise Spiral Traversal of Binary Tree
Given a Binary Tree. The task is to print the circular clockwise spiral order traversal of the given binary tree. For the above binary tree, the circular clockwise spiral order traversal will be 1, 4, 5, 6, 7, 2, 3. Examples: Input : 10 / \ 12 13 / \ 14 15 / \ / \ 21 22 23 24 Output : 10, 24, 23, 22
15+ min read
C program to create a pendulum clock using graphics
In this article, it is discussed how to design a Pendulum Clock in the C programming language using Graphics. Approach: Create two rectangles, one inside the other, using the rectangle() function to act as the outer outline of the clock & the other is the inner outline of the clock.Color the spa
3 min read