title: How to add names of files of a directory to an array structure using c
body: <p>Can anyone tell me how do I save the paths of directories in a directory using array of structures in C. In the code below can anyone tell where is the change needed?</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;sys/dir.h&gt;
  #include &lt;locale.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;string.h&gt;
  
  #define FALSE 0
  #define TRUE ! FALSE
  
  typedef struct{
      char *path;
  }filepath;
  
  struct stat sb;
  extern int alpahsort();
  int dir_detect(char *name);
  
  int main (int argc, char *argv[])
  {
      filepath my_array_path[100];
      char *each_name;
      const char *pathname=NULL;
      char success; int ret=0;
      struct direct **files;
      int j=0,i,count,count_dir;
      int file_select();
      if (argc != 2) {
          fprintf(stderr, "Usage: %s &lt;pathname&gt;\n", argv[0]);
          exit(EXIT_FAILURE);
      }
      printf("%s\n",argv[1]);
      pathname=argv[1];
      printf("%s\n",pathname);
      DIR *dp;
      struct dirent *ep;
      dp = opendir (pathname);
  
      count = scandir(pathname, &amp;files, file_select, alphasort);
  
      if (dp != NULL)
      {
          while ((ep = readdir (dp))!=NULL){
              printf("the number of files=%d\n",count);
              char *buffer;
  
  
              //from here ....
              //my_array_path[i].path=malloc(strlen(buffer+1));
              //strcpy(my_array_path[i].path,buffer);
              my_array_path[i].path=strdup(ep-&gt;d_name);
              printf("the name of the file is %s\n",my_array_path[i].path);
              // to here......
  </code></pre>
  
  <p>I wanted to know what I am doing is correct or not. Other code follows.</p>