3

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?

aurelienC
  • 1,113
  • 3
  • 12
  • 22
  • http://stackoverflow.com/q/16709946/1606345 – David Ranieri Jan 31 '16 at 09:41
  • I honestly don't understand the problem. If you're a beginner, pointers may be confusing and something like you suggest might be useful, but after getting used to it, it's no big deal. Pointers are always used in similar patterns and after a while you'll learn them. – cadaniluk Jan 31 '16 at 09:41
  • 3
    Use variable naming conventions, and code-comments above the pointer's declaration describing its properties (i.e. whether it will own what it is pointing to, and whether it may be pointing to the first of an array) – M.M Jan 31 '16 at 09:47
  • 1
    Triple indirection is confusing, there is no way around that. Try to avoid triple indirection. – fuz Jan 31 '16 at 09:52

3 Answers3

1

Meaning of pointer is always the same. It is always actually pointing at a single object only. Therefore, A pointer is a variable pointing to a single place in memory


As for your different examples

When you do something like

char* char_array = "abcd";
int* int_array = malloc(5 * sizeof(*int_array));

In this also, the pointer char_array is pointing to the single char, which is a of the whole string "abcd". It so happens, that due to the way it is initialized (string literal), there are characters stored in the next memory locations.


The pointer in the above example is actually the same as char_ptr below
char* char_array = "abcd";
char* char_ptr = char_array;

If you you it like this

printf(" %c", char_ptr);

It will print a

If you do something like

printf(" %s", char_ptr);

It will print the whole string "abcd"

Here you can see that char_ptr also is acting how char_array would have in a printf() statement,

Haris
  • 12,120
  • 6
  • 43
  • 70
  • 1
    I don't think this is a very useful response; it's common for a pointer to point to an object which is the first in a series of objects (aka. an array) and OP is asking how we know whether or not that case is happening. E.g. how do you know if `printf("%s", char_ptr);` is safe when it might be that `char_ptr` had been declared as `char x; char *char_ptr = &x;` – M.M Jan 31 '16 at 09:48
  • @M.M, ok. I feel the OP asked how to know whether they are different in theory. Which I posted that they are not different in theory. – Haris Jan 31 '16 at 09:51
  • 1
    Your are right that the *nature* of a pointer is defined as such, but OP is more interested in the different *roles* that can be attributed to pointers. By analogy, all humans are made the same but we have different jobs. – coredump Jan 31 '16 at 09:52
  • They are different in theory though, the situation of pointing to 1 object only needs different code handling to the situation of potentially pointing to 1 object which is the first in a series – M.M Jan 31 '16 at 09:52
  • @M.M, There are not that different in theory in case of C standards. All pointers are just pointing to an object. – Haris Jan 31 '16 at 09:53
1

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.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
0

This is a lot about code style, so there could be different answers and they all can be right, but presenting a different style - or point of view - if you will.

My advice would be to never go beyond two *. And if I remember well my C coding, I used two * only for void ** type.

In all other cases, use typedef (not macros!) and make a good structure of your objects.

With typedef, create name for each level. So, if you start with a type CELL, and you want array of these, call it e.g. CELL_LIST, which would be an array of (a pointer to) CELLs. You want array of CELL_LIST? Name it BOARD for example.

So CELL_LIST is pointer to CELL. BOARD is pointer to CELL_LIST (could be named ROW too). Need another dimension? Call it BOARD_LIST or BOARD_3D ...

Wapac
  • 4,058
  • 2
  • 20
  • 33
  • 1
    My advice would be to never use pointer typedefs ; the reader has to build an accurate mental model of the memory layout and having the `*` being obscured interferes with that – M.M Jan 31 '16 at 09:50