C: Formatting of pointers

Problems has arose when I was attempting to make a forward declaration for my function which included double pointers in the parameters, which was a function intended to store strings in an array.

First, from solving one of my own bug, it is does not work well when an asterisk is being placed after the variable name. Variable declarations after the wrongly formatted pointer declaration would not be valid.

Secondly,referencing from:
The way to read a declaration in C goes like:

Start at the variable name (or innermost construct if no identifier
is present.

Look right without jumping over a right parenthesis; say
what you see.

Look left again without jumping over a parenthesis; say
what you see.

Jump out a level of parentheses if any.

Look right;say what you see.

Look left; say what you see.

Continue in this manner until you say the variable type or return type.

So for the case

First we read vtable
Then we see nothing on the right
Then we see a pointer on the left
Now we jump out
Then we see an array on the right, so an array of pointers up to this point
Then we look left, we see a pointer
This means we have a pointer pointing to an array of pointers
Jumping out, we have nothing left but the type int
Now we have a pointer pointing to an array of pointers of returning type int

This did help me to get along with reading C declarations including pointers, however, it did not solve my problem of how to match the function syntax correctly.

During lecture, we were being advised that we are not encouraged to use double pointers as a return type,as it would return arbitrary results. With the new taught knowledge of using static variables, I have decided to leave my double pointers to an array of pointers with fixed size as global variables.

This turned out to be a much clearer way of solving the problem of storing an array of strings in C.


Popular Posts