title: C Lang - Struct Array and Segmentation fault (core dumped)
body: <p>I have written a simple code to read a directory then sort the files by last modified time,but the following code will crash because Segmentation fault (core dumped)，（occur when try to access the lsf[0] and lsf[1] ），how to solve the problem，please help，thanks：</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;time.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct {
    char *name;
    unsigned long lasted;
  } sort_file;
  
  char *make_path(char *path) {
    char *endp = path + strlen(path);
    *endp++ = '/';
    return endp;
  }
  
  int main(int arc, char **argv) {
  
    const char *n_dir = "/home/psycho/Pictures";
    char *dir_name = strdup(n_dir);
  
    char *endp = make_path(dir_name);
  
    DIR *dir;
    struct dirent *ent;
    struct stat attr;
  
    int size = 150;
    int count = 0;
    sort_file **lsf = malloc(size * sizeof(sort_file *));
    printf("[Logging] : %d =&gt; %d  \n", sizeof(sort_file), sizeof(sort_file *));
    if ((dir = opendir(n_dir)) != NULL) {
      while ((ent = readdir(dir)) != NULL) {
  
        if (ent-&gt;d_name[0] == '.') {
          continue;
        } else if (ent-&gt;d_type == DT_DIR) {
          strcpy(endp, ent-&gt;d_name);
          stat(dir_name, &amp;attr);
  
          lsf[count] = malloc(sizeof(sort_file));
          lsf[count]-&gt;lasted = (unsigned long)attr.st_mtime;
          lsf[count]-&gt;name = malloc((strlen(ent-&gt;d_name) )* sizeof(char));
  
          sprintf(lsf[count]-&gt;name, "%s", ent-&gt;d_name);
          printf("[Logging] : --- %lu --- %d --- %s  \n", lsf[count]-&gt;lasted,
                 count, lsf[count]-&gt;name);
  
  
          count++;
  
        }
      }
      closedir(dir);
  
    } else {
      return 0;
    }
  
    while (count--) {
      printf("[Logging] : --- %lu --- %d --- %s  \n", lsf[count]-&gt;lasted, count,
             lsf[count]-&gt;name);
    }
    return 0;
  }
  </code></pre>