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
<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
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 Find Compound Interest
What is 'Compound interest'? Compound interest is the addition of interest to the principal sum of a loan or deposit, or in other words, interest on interest. It is the result of reinvesting interest, rather than paying it out, so that interest in the next period is then earned on the principal sum
2 min read
C++ program for Complex Number Calculator
Pre-Requisites: Complex Numbers, Mathematics, Object-oriented Programming This is a Complex Number Calculator which performs a lot of operations which are mentioned below, to simplify our day-to-day problems in mathematics. The implementation of this calculator is done using C++ Programming Language
15+ min read
C++ Program to Modify a matrix by rotating ith row exactly i times in clockwise direction
Given a matrix mat[][] of dimensions M * N, the task is to print the matrix obtained after rotating every ith row of the matrix i times in a clockwise direction. Examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Output:1 2 36 4 58 9 7Explanation:The 0th row is rotated 0 times. Therefore, t
3 min read