ポインターは非常に多くのことに役立つため、特定のコード行でポインターが何を意味するのか理解できない場合があります。
たとえば、一連の要素を表すためにポインターを使用することがあります。
char* char_array = "abcd";
int* int_array = malloc(5 * sizeof(*int_array));
また、ポインターを使用して、ヒープに単一のオブジェクトを割り当てたり、ある要素から別の要素を指すようにしたりすることもあります。
int a = 5;
int* int_ptr = &a;
struct item* an_item = malloc(sizeof(*an_item));
両方が衝突を使用すると、連続するポインターが読み取れなくなります。
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;
or マクロを使用typedef
して、参照または malloc されたものとして使用されるポインターを表す型を作成することを考えました。
場合によっては読みやすさが向上するため、これは諸刃の剣になる可能性がありますが、コードを難読化することにもなります。
ポインターの意味が理解しやすいコードを作成するには、どのような方法をお勧めしますか?