title: C segmentation fault when I try to printf just after printing the first value inside function
body: <p>I am getting <em>segmentation fault</em> when I try to use <code>printf</code> on <code>*buffer[i]</code> in <code>readWav()</code> function. And <code>buffer</code> was able to correctly pass to <code>main()</code> and I was able to check values in <code>main</code> with <code>datas[i]</code>. I simply copied the <code>printf</code> in <code>main()</code> and had to replace <code>datas[i]</code> with <code>*buffer[i]</code> then it compiles fine. It prints the first value in <code>readWav()</code> and then <em>SegFault</em> occurs.</p>
  
  <p>How do I do this in <code>readWav()</code> - What is wrong with <code>*buffer[i])</code> - in other words, if <code>datas[i]</code> refers to actual values in <code>main()</code> what would that be in <code>readWav()</code> in terms of <code>buffer</code>?</p>
  
  <p>Update:
  <code>main()</code> will pass <code>datas</code> to other functions still to be added on. That is why I tried printing it in <code>readWav()</code> thinking it will be very similar in how values are passed on to other functions - please correct me if I am wrong?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "sndfile.h"
  
  int readWav(const char *const fname, long *numFrames, int *sRate, float **buffer);
  
  int main(int argc, char *argv[])
  {
      int   sRates, sRatem, ret;
      long  nSamples = 0, nSamplem;
      float *datas, *datam;
  
      printf("Read Test\n");
      if (argc != 3) {
          fprintf(stderr, "Expecting two wav file as argument\n");
          return 1;
      }
  
      ret = readWav(argv[1], &amp;nSamples, &amp;sRates, &amp;datas);
      if (ret != 0) {
          printf("Error\n");
          return 1;
      }
      // Output Info
      printf("Read %ld frames from %s, Sample rate: %d, Length: %fs\n",
          nSamples, argv[1], sRates, (float)nSamples/sRates);
  
      for (i=0; i &lt; nSamples ; i++) {
          printf("%d\t %f\n", i, datas[i]);
      }
  
      free(datas);
  
      return 0;
  //Cleanup etc:
  }
  
  int readWav(const char *const fname, long *numFrames, int *sRate, float **buffer)
  {
      // Open sound file
      SF_INFO sndInfo;
  
      if ((sRate == NULL) || (numFrames == NULL) || (buffer == NULL)) {
          fprintf(stderr, "Invalid arguments passed to readWav()\n");
          return 1;
      }
  
      SNDFILE *sndFile = sf_open(fname, SFM_READ, &amp;sndInfo);
      if (sndFile == NULL) {
          fprintf(stderr, "Error reading source file '%s': %s\n", fname, sf_strerror(sndFile));
          return 1;
      }
  
      // Allocate memory
      *buffer = malloc(sndInfo.frames * sndInfo.channels * sizeof(float));
      if (*buffer == NULL) {
          fprintf(stderr, "Could not allocate memory for file\n");
          sf_close(sndFile);
  
          return 1;
      }
  
      *sRate = sndInfo.samplerate;
      // Load data
      *numFrames = sf_readf_float(sndFile, *buffer, sndInfo.frames);
      // Check correct number of samples loaded
      if (*numFrames != sndInfo.frames) {
          fprintf(stderr, "Did not read enough frames for source\n");
          sf_close(sndFile);
          free(*buffer);
      }
      else {
          printf("Successfully read file\n");
          *numFrames = sndInfo.frames;
      }
      // Output Info
      printf("Read %ld frames from %s, Sample rate: %d, Length: %fs\n",
          *numFrames, fname, *sRate, (float)*numFrames/sndInfo.samplerate);
  
      for (i=0; i &lt; sndInfo.frames ; i++) {
          printf("%d\t %f\n", i, *buffer[i]);
      }
  
      sf_close(sndFile);
      return(0);
  }
  </code></pre>