title: Splint: "Value strings[] used before definition" with dynamic array
body: <p>I'm using a dynamic array of strings in C:</p>
  
  <pre><code>char** strings;
  </code></pre>
  
  <p>I initialize it:</p>
  
  <pre><code>int max = 10;
  strings = malloc(sizeof(char*) * max);
  </code></pre>
  
  <p>And copy a couple of dummy strings:</p>
  
  <pre><code>char* str = "dummy";
  for (int i = 0; i &lt; max; i++) {
    strings[i] = malloc(strlen(str) + 1);
    strncpy(strings[i], str, strlen(str) + 1);
  }
  </code></pre>
  
  <p>Yet when I try to print this:</p>
  
  <pre><code>for (int i = 0; i &lt; max; i++)
    printf("array = %s", strings[i])
  </code></pre>
  
  <p>I get this error from Splint:</p>
  
  <pre><code>Value strings[] used before definition
    An rvalue is used that may not be initialized to a value on some execution
    path. (Use -usedef to inhibit warning)
  </code></pre>
  
  <p>Checking for <code>NULL</code> like this will not help:</p>
  
  <pre><code>for (int i = 0; i &lt; max; i++)
    if (strings[i] != NULL)
      printf("array = %s", strings[i])
  </code></pre>
  
  <p>since <code>strings[i]</code> is still used "before definition".</p>
  
  <p>Any ideas on how to solve this?</p>
  
  <p><strong>Edit</strong>: Will try this with a linked list instead, I think.</p>
  
  <p>Also, complete code listing:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      char** strings;
      int i;
      int max = 10;
      char* str = "hello";
  
      // Dynamic array with size max
      strings = malloc(sizeof(char*) * max);
  
      // Abort if NULL
      if (strings == NULL)
          return (-1);
  
      // Define strings
      for (i = 0; i &lt; max; i++)
      {
          strings[i] = malloc(strlen(str) + 1);
  
          // Abort if NULL
          if (strings[i] == NULL)
          {
              // Undetected memory leak here!
              free(strings);
              return (-1);
          }
  
          strncpy(strings[i], str, strlen(str) + 1);
      }
  
      // Print strings
      for (i = 0; i &lt; max; i++)
      {
          if (strings[i] != NULL)
              printf("string[%d] = %s\n", i, strings[i]);
      }
  
      // Free strings
      for (i = 0; i &lt; max; i++)
      {
          if (strings[i] != NULL)
              free(strings[i]);
      }
      free(strings);
  
      return 0;
  }
  </code></pre>