Pointers are useful for so many things, that it is sometimes impossible to understand what they mean in a particular line of code.
For exemple sometimes you use pointers to represent a succession of elements:
char* char_array = "abcd";
int* int_array = malloc(5 * sizeof(*int_array));
And sometimes you use pointers to allocate a single object on the heap or to make one element to points to another:
int a = 5;
int* int_ptr = &a;
struct item* an_item = malloc(sizeof(*an_item));
When both use collides, successive pointers become unreadable:
struct cell** board;
// Does this represent a succession of cell allocated on the heap,
// a succession of pointers to uniques cells (like an array),
// a succession of pointers to multiples cells (like a two dimensional array)?
// Of course the more you add pointers the more it becomes confusing.
struct cell*** board;
I thinked about using typedef
or macros to make a type that represent a pointer used as a reference or as something that have been malloc-ed.
This could be double-edged because in some cases I will gain readability, but it will also obfuscate the code.
What do you recommend to produce code where the meaning of pointers is easier to understand?
What do you recommend to produce code where the meaning of pointers is easier to understand?
It is indeed true that the meaning of pointers can be unclear to an outsider of the code. I recommend prefixing the variable names to give them more meaning.
See: https://en.wikipedia.org/wiki/Hungarian_notation#Examples
Of course you don't have to follow this example in detail. You can find multiple way's to prefix or make up your own. As long as you explain it somewhere you should be good.