title: pointer without a malloc
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/stat.h&gt;
  
  int main(){
          struct stat *something;
          stat("/etc/profile", something);
          printf("%d\n", something-&gt;st_gid);
          free(something);
          return 0;
  }
  $ ./a.out
  Segmentation fault
  </code></pre>
  
  <p>I learned <a href="https://stackoverflow.com/questions/3138600/correct-use-of-stat-on-c">from this post</a> that I need to allocate memory using malloc, so I changed it to as below, and it works:</p>
  
  <pre><code>-       struct stat *something;
  +       struct stat *something = malloc(sizeof(struct stat));
  </code></pre>
  
  <p>In a prior but related exercise, I did not use a malloc, and it had worked. 
  I am lost!
  Why don't I need malloc for the line "<strong>*struct dirent <em>b;</em></strong>" below?</p>
  
  <p>Or, to rephrase, how can we know the payload is too much and to use malloc? </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;dirent.h&gt;
  
  int main(int argc, char *argv[]){
  
          if (argc != 2){
                  printf("Error. Syntax: ls &lt;somefolder&gt; \n");
                  return 1;
          }
          DIR *a = opendir(argv[1]) ;
          if (a == NULL){
                  printf("error. cannot open %s\n", argv[1]);
                  return 1;
          }
  
          // - malloc question about this very next line
          struct dirent *b; 
          while (( b = readdir(a)) != NULL){
                  printf("%s %lu\n", b-&gt;d_name, b-&gt;d_ino);
          }
  
          int closing = closedir(a);
          printf("in closing, status is %d\n", closing);
          return 0;
  }
  </code></pre>
  
  <p>Newcomer to C, clueless too - please be gentle! :) </p>