title: Segmentation fault(core dumped) in recursively listing of directories and files in C, Linux
body: <p>so basically the issue appears when I try to run a function which <strong>recursively prints the paths for files and directories</strong>. The issue that I am getting in terminal when I run the program is "<strong>Segmentation fault (core dumped)</strong>".</p>
  
  <p>Any suggestions how I can solve this?</p>
  
  <p>EDIT: MAX_LEN defined as 2048.</p>
  
  <p>This is the code:</p>
  
  <pre><code>void list_recursive(char* path){
      DIR* dir;
      struct dirent *dirent;
      char * name = malloc(sizeof(char) * MAX_LEN);
  
      dir = opendir(path);    
      if(dir != NULL){
          printf("SUCCESS\n");
          while((dirent = readdir(dir)) != NULL){
              if(strcmp(dirent-&gt;d_name, ".") != 0 &amp;&amp; strcmp(dirent-&gt;d_name, "..") != 0){
                  sprintf(name, "%s/%s", path, dirent-&gt;d_name);
                  printf("%s\n", name);
              }
          }
          free(name);
          if(dirent-&gt;d_type == DT_DIR){
              list_recursive(dirent-&gt;d_name);
          }   
          closedir(dir);  
      }
      else {
          printf("ERROR\n");
          printf("invalid directory path\n");
      }
  }
  </code></pre>