title: Dereferencing an array value via pointer to the array
body: <p>I'm probably missing something obvious, but my C is pretty rusty and I'm not having any luck making sense of this.  I have a loop where I want to iterate over an array of <code>uint64_t</code> values coming from libdvdnav and then format the values and insert them into a string.</p>
  
  <p>The header for libdvdnav defines the function I'm calling thusly:</p>
  
  <pre><code>uint32_t dvdnav_describe_title_chapters(dvdnav_t *self, int32_t title, uint64_t **times, uint64_t *duration);
  </code></pre>
  
  <p>Here's how I'm defining the variables used and executing the call (<code>dvdnav</code> and <code>args[0]</code> are defined and initialized elsewhere):</p>
  
  <pre><code>uint64_t *times;
  uint64_t duration;
  uint32_t times_array_len;
  
  times_array_len = dvdnav_describe_title_chapters(dvdnav, atoi(args[0]), &amp;times, &amp;duration);
  </code></pre>
  
  <p>The code below seems to work, and compiles &amp; runs w/o error, but of course only the first value in the array is inserted:</p>
  
  <pre><code>int i = 0;
  uint64_t a_time = times[0];
  
  while(i &lt; times_array_len){
  
      char time_json[100];
      sprintf(time_json, "{\"chapter\":\"%d\",\"time\":\"%u\"},", i, a_time);
      strcat(payload, time_json);
      i++;
  } 
  </code></pre>
  
  <p>If I modify this to select each value in the array it still compiles clean, but throws a segfault at runtime:</p>
  
  <pre><code>int i = 0;
  
  while(i &lt; times_array_len){
  
      char time_json[100];
      sprintf(time_json, "{\"chapter\":\"%d\",\"time\":\"%u\"},", i, times[i]);
      strcat(payload, time_json);
      i++;
  } 
  </code></pre>
  
  <p>I thought maybe there was something in one of the array elements that was a problem (a too-large value, unexpected, NULL, etc.) but even if I replace the variable <code>i</code> with a known-reasonable element (say, <code>0</code>) it still segfaults.</p>
  
  <p>I'm sure there's countless other improvements to be made here (safer allocations, overflow protection, etc.) but the part I'm struggling to decipher is getting those values out of the array an into my formatted string.</p>