Skip to main content

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
int(*(*vtable)[])()

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.


Comments

Popular posts from this blog

Understanding database [6] : Clustered Index

A clustered index is what is good for a range search over a range of search key values, and the index entries and the rows are ordered the same way, which is different from unclustered indexes (secondary indexes). To use a clustered index, we use the index to locate the first index entry at the start of the range, which where the first row is located at. If the index is clustered, subsequent rows will be stored in successive locations with the ordering , therefore it may minimize the page transfers and maximizes cache hits. For one table, there may only be one clustered index, whereas there can be as many unclustered indexes as you may want  to create. Unclustered indexes aren ’ t as good as clustered, but they might become necessary when it comes to finding for other attributes apart from the primary key. Smaller topics : Dense index & Sparse index When we say sparse index, we mean that there is an index entry for each page of the data file. With this structuring

Understanding database [9] : Choosing indexes

Understanding database 9 : Choosing indexes When choosing indexes, we choose the best plan that suits for the queries, and look for additional indexes that may potentially upgrade upon that. Before creating, we must also consider the impact on updates in the workload, such that indexes take disk space. For a query, the WHERE clause are the main focus point to make indexes on, where exact matches suggest a hash index and range queries suggest a tree index. Clustering is extremely helpful when it comes to range queries, and may also help with equality queries if there are duplicates. Search keys with multiple attribute should be considered if a WHERE clause contains multiple conditions, and the order of attributes is important for range queries. Searching may become ‘index-only’ with such indexes.