The Startup
Published in

The Startup

How to Easily Verbalize Any C Variable Type Declaration

Here’s a couple of pointers.

Part of what makes C incredibly powerful is its complex type declarations. These statements can become infinitely complex, making it a necessity that we know how to express them into plain English.

A sample complex C variable type declaration

Like in most things programming, this is done by merely following an algorithm.

1. Identify and Locate the Variable Name

This is typically a name within the type declaration that is not a reserved word.

2. Move Left to Traverse Pointers

From the position of the variable name, move left until you read an opening parenthesis, ‘(‘, or the start of the type declaration, exluding the left-most type. For every ‘*’ you encounter, add the term “pointer to” to the variable declaration.

3. Move Right to Traverse Arrays and Functions

Resuming from the end of step 2, move right until you read a closing parenthesis, ‘)’, or the end of the variable declaration.

If you encounter brackets, add “array of length {length} of“ to the type declaration.

If you encounter parentheses, there are three possibilities:

  1. If the parentheses are empty, ‘()’, add “a function with unknown parameters returning” to the declaration.
  2. If the parentheses read ‘(void)’, add “a function with taking no parameters returning”.
  3. Otherwise, add “a function taking ({list param types}) returning”.

Repeat steps 2 and 3 until you have reached both ends of the variable declaration.

4. Recognize Final Type

Finally, add the left-most type to the variable’s type declaration. This is always a reserved type, struct, union, or class, or other pre-defined type.

Example

Imagine we want to verbalize the following variable declaration:

char (*(**foo)[3])[10);Step 1: "Declare foo to be of type"
Step 2: "pointer to pointer to"
Step 3: "array length 3 of"
Step 2 (repeat): "pointer to"
Step 3 (repeat): "array length 10 of"
Step 4: "char"

In the end, we find that

char (*(**foo)[3])[10);

means

Declare foo to be of type pointer to a pointer to array length 3 of pointer to array length 10 of char.

More Difficult Example

unsigned int (*(**(*foo)(int , volatile short ))[4][6])[8];Step 1: "Declare foo to be of type"
Step 2: "pointer to"
Step 3: "function taking (int, volatile short) returning"
Step 2 (repeat): "pointer to pointer to"
Step 3 (repeat): "array length 4 of array length 6 of"
Step 2 (repeat): "pointer to"
Step 3 (repeat): "array length 8 of"
Step 4: "unsigned int

Declare foo to be of type pointer to function (int, volatile short) returning pointer to pointer to array length 4 of array length 6 of pointer to array length 8 of unsigned int.

Try for Yourself

Play around with this awesome tool to practice your C variable declaration verbalization skills!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Blake Sanie

Blake Sanie

51 Followers

‌‌‎Inquisitive student.‎‌‌ Aspiring engineer. Photography enthusiast. Curious stock trader. blakesanie.com