title: Reading directories using readdir_r
body: <p>I've been checking and fighting with this procedure without success for quite a while, hope you can help me out.</p>
  
  <p>The idea is to read the directory stored in <code>c_Localpath</code> and copy the read dirs into <code>c_namesLocal</code> to return them.</p>
  
  <p>Am I doing something wrong in my implementation? The program breaks on the <code>strcpy</code>, and I have no clue why.</p>
  
  <pre><code>DIR* ptr_dir = opendir(c_Localpath);
  
  char** c_namesLocal = calloc(1, 256);
  
      size_t numElements = 0;
      int returnCode =0;
      struct dirent ptr_PrevDirEntry;
      struct dirent* ptr_DirEntry = NULL;
      returnCode = readdir_r(ptr_dir, &amp;ptr_PrevDirEntry,
              &amp;ptr_DirEntry);
  
      while ((returnCode ==0) &amp;&amp; (ptr_DirEntry != NULL)) {
          char* name = c_namesLocal[numElements];
          strcpy(name, ptr_DirEntry-&gt;d_name);
          ptr_PrevDirEntry = *ptr_DirEntry;
          returnCode = readdir_r(ptr_dir, &amp;ptr_PrevDirEntry,
                  &amp;ptr_DirEntry);
          numElements++;
          c_namesLocal = realloc(c_namesLocal, 256 * numElements);
      }
  </code></pre>