Clockwise/Spiral Rule in C/C++ with Examples

Do you know the Clockwise/Spiral Rule in C/C++?

If your answer is “NO”, then don’t worry in this article you will learn about the Clockwise/Spiral Rule. But, if you already know the Clockwise/Spiral Rule, then this blog post will help you in quick revision.

Let’s Begin.

The Clockwise/Spiral technique is originally introduced by “David Anderson”. This technique enables the C program to parse in their mind any C declaration! It means this technique helps you in understanding a complex declaration and easily you can parse complex C declaration.

Three simple steps of the Clockwise/Spiral Rule in C:

There are the following steps which you need to follow. In beginning, you find these steps complicated but believe me after solving some examples it becomes easy!

➤ Start from the name of the variable, move in a spiral/clockwise direction. When encountering the below-mentioned elements replace them with the corresponding English statements:
  [X] or [] => Array X size of… or Array undefined size of…

(type1, type2) => function passing type1 and type2 returning…
* => pointer(s) to…

➤ Repeat until all tokens have been covered.

➤ Always resolve anything in parenthesis first!

 

So let’s see some examples, we will move from easy to hard!

Simple declaration:

Example 1:

int *ptr;

Now Question is, What is ptr?

So, here we can use above mentioned techniques, we move in a spiral clockwise direction starting with `ptr’ and the first character seen is a `*’,  so… ptr is a pointer to…

Continue in a spiral direction and we see the end of the line (the `;’), so keep going and we get to the type `int’,  so… “ptr is a pointer to int”

 

Example 2:

int * const ptr;

Now Question is, What is ptr?

We need to implement the same rule here, we move in a spiral clockwise direction starting with `ptr’ and the first we see is a const qualifier,  so… ptr is a constant

Continue in a spiral direction and we see a `*’,  so… ptr is a constant pointer to

Continuing in a spiral fashion we see the end of the line (the `;’), so keep going and we get to the type `int’,  so… “ptr is a constant pointer to int”.

 

Video Tutorial (Hindi):

 

Subscribe to the channel for more videos and continue reading for more information.

 

Pointers array declaration:

Example:

int*ptr[10];

Question is, What is ptr?

     +-------+
     | +-+   |
     | ^ |   |
int *ptr[10];
 ^   ^   |   |
 |   +---+   |
 +-----------+

 

Start from the ptr and move in a spiral clockwise direction. The first character we see is a `[' so, that means we have an array, so… ptr is an array 10 of

Continue moving in a spiral clockwise direction, and the next character we encounter is the `*‘ so, that means we have pointers, so… ptr is an array 10 of pointers to

Keep moving in a spiral direction and we see the end of the line (the `;’), so keep going and we get to the type `int’, so…”ptr is an array 10 of pointers to int

⚡Note: Mentioned steps are not a universal rule there are some cases where they fail.

 

Pointer to Function declaration:

Example:

int*(*fp)( int, float *);

Again the same question is, What is fp?

     +--------------------+
     | +---+              |
     | |+-+|              |
     | |^ ||              |
int *(*fp)( int, float *);
 ^   ^ ^  ||              |
 |   | +--+|              |
 |   +-----+              |
 +------------------------+

Now I believe you have memorized the steps, start from fp and move in a spiral clockwise direction the first thing we see is a `)'; therefore, fp is inside the parenthesis, so we continue the spiral inside the parenthesis and the next character seen is the `*', so… fp is a pointer to

Now we are outside of the parenthesis and moving in a spiral clockwise direction, we see the `('; therefore, we have a function and the argument is int and float* so… fp is a pointer to a function passing an int and a pointer to float returning

Continuing in a spiral fashion, we then see the `*‘ character, so… `fp is a pointer to a function passing an int and a pointer to float returning a pointer to…

Keep continuing in a spiral fashion we see the end of the line (the `;’), so keep going and we get to the type `int’,  so… “fp is a pointer to a function passing an int and a pointer to float returning a pointer to an int”.

 

Recommended Post