title: C - word array keeps getting overridden
body: <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;stdlib.h&gt;
  
  // This program is going to scan all files in the current directory. It will make a tree for every folder
  // and the folder will have a subsection of files in the tree format. YES SORTING!
  
  char **word;
  int coun = 0;
  
  void printdir(char *dir, int depth)
  {
      DIR *dp;
      struct dirent *entry;
      struct stat statbuf;
  
      if ((dp = opendir(dir)) == NULL) 
      {
          fprintf(stderr,&quot;cannot open directory: %s\n&quot;, dir);
          return;
      }
      
      chdir(dir);
      
      while((entry = readdir(dp)) != NULL) 
      {
          lstat(entry-&gt;d_name,&amp;statbuf);
          
          if (S_ISDIR(statbuf.st_mode)) // Check if it's a directory
          {
              /* Found a directory, but ignore . and .. */
              if (strcmp(&quot;.&quot;, entry-&gt;d_name) == 0 || strcmp(&quot;..&quot;, entry-&gt;d_name) == 0)
              {
                  continue;
              }
  
              word[coun] = (&quot;%s&quot;, entry-&gt;d_name); // Put the file name in the array.
              coun++;
              printf(&quot;- %*s%s\n&quot;, depth, &quot;&quot;, entry-&gt;d_name); // Print the name of the dir entry.
              /* Recurse at a new indent level */
              printdir(entry-&gt;d_name, depth + 1);
          }
          else 
          {
              word[coun] = (&quot;%s&quot;, entry-&gt;d_name); // Put the file name in the array.
          coun++;
              printf(&quot;%*s - %s\n&quot;, depth, &quot;&quot;, entry-&gt;d_name); // This will print the file.
          }
      }
  
      chdir(&quot;..&quot;);
      closedir(dp);
  }
  
  int main(int argc, char* argv[])
  {
      word = calloc(1000, sizeof(*word));
      printdir(&quot;.&quot;, 0);
      printf(&quot;now, print the words in the order they were printed.\n&quot;);
  
      for (int i = 0; i &lt; coun; ++i)
      {
          printf(&quot;%s\n&quot;, word[i]);
      }
  
      exit(0);
  }
  </code></pre>
  <p>My main aim for this code is to make a tree structure of the files that are currently in the directory. When I run it, I get this output.</p>
  <pre><code>- hw2
    - tree
    - Makefile
    - ls.c
    - tree.c
    - find.c
  - hw1
    - grep.c
    - factor.c
    - uniq.c
    - monster.c
    - sort.c
   - .nfs0000000006c543ea0000e073
   - tree.c
   - tree
  now, print the words in the order they were printed.
  hw2
  grep.c
  
  
  
  
  
  
  hw1
  grep.c
  factor.c
  uniq.c
  monster.c
  sort.c
  .nfs0000000006c543ea0000e073
  tree.c
  tree
  
  </code></pre>
  <p>The tree works fine, but I still need to sort the files afterward. My plan is to put all of the file names into the global words array, then discern between folders and files, and print in the same format, but ordered alphabetically case-insensitive.
  I check the array, but the hw2 folder and its files gets overridden completely. I don't understand why this is happening because it should be working fine.
  Does anyone know a fix or maybe a better way to do this?</p>