title: Storing a Dynamic Array of Structures
body: <p>I have been working on a project which utilizes a dynamic array of structures. To avoid storing the number of structures in its own variables (the count of structures), I have been using an array of pointers to the structure variables with a <code>NULL</code> terminator.</p>
  
  <p>For example, let's say my structure type is defined as:</p>
  
  <pre><code>typedef struct structure_item{
      /* ... Structure Variables Here ... */
  } item_t;
  </code></pre>
  
  <p>Now let's say my code has <code>item_t **allItems = { item_1, item_2, item_3, ..., item_n, NULL };</code> and all <code>item_#</code>s are of the type <code>item_t *</code>.</p>
  
  <p>Using this setup, I then do not have to keep track of another variable which tells me the total number of items. Instead, I can determine the total number of items as needed by saying:</p>
  
  <pre><code>int numberOfStructures;
  for( numberOfStructures = 0;
       *(allItems + numberOfStructures) != NULL;
       numberOfStructures++
    );
  </code></pre>
  
  <p>When this code executes, it counts the total number of pointers before <code>NULL</code>.</p>
  
  <p>As a comparison, this system is similar to C-style strings; whereas tracking the total number of structures would be similar to a Pascal-style string. (Because C uses a <code>NULL</code> terminated array of characters vs. Pascal which tracks the length of its array of characters.)</p>
  
  <p>My question is rather simple, is an array of pointers (pointer to pointer to struct) really necessary or could this be done with an array of structs (pointer to struct)? Can anybody provide better ways to handle this?</p>
  
  <p><strong>Note:</strong> it is important that the solution is compatible with both C <strong>and</strong> C++. This is being used in a wrapper library which is wrapping a C++ library for use in standard C.</p>
  
  <p>Thank you all in advance!</p>