title: Pointer Based Data Structures
body: <p>So my homework instructs me to read in a .WAV file and fill out the header and data according to their respective endians. I believe I have malloced enough memory for my header and data struct, but when I run the program, I end up with a Segmentation Fault 11. I can't seem to find where I went wrong. Any help would be appreciated!</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  #include "prog9.h"
  
  /*
   * little_endian_2 - reads 2 bytes of little endian and reorganizes it into big endian
   * INPUTS:       fptr - pointer to the wav file
   * OUTPUTS:      none
   * RETURNS:      the data that is converted to big endian
   */
  int little_endian_2(FILE *fptr)
  {
      int count;
      char temp[2];
      count = 0;
      while (count &lt; 2)
      {
          fscanf (fptr, "%s", temp);
          count++;
  
  
      }
  
      char holder;
  
      holder = temp[1];
      temp[1] = temp[0];
      temp[0] = holder;
  
      printf ("%c", temp[0]);
      printf("%c", temp[1]);
      count = atoi(temp);
      return count;
  }
  
  /*
   * little_endian_4 - reads 4 bytes of little endian and reorganizes it into big endian
   * INPUTS:       fptr - pointer to the wav file
   * OUTPUTS:      none
   * RETURNS:      the data that is converted to big endian
   */
  int little_endian_4(FILE *fptr)
  {
      int count = 0;
      char temp[4];
  
      while (count &lt; 4)
      {
          fscanf (fptr, "%c", temp);
          count++;
  
      }
  
      char holder;
  
      holder = temp[3];
      temp[3] = temp[0];
      temp[0] = holder;
  
      holder = temp[2];
      temp[2] = temp[1];
      temp[1] = holder;
  
      count = atoi(temp);
      return count;
  }
  
  /*
   * read_file  - read the wav file and fill out the wav file struct
   * INPUTS:      wavfile - a string that contains the name of the file
   * OUTPUTS:     none
   * RETURNS:     the pointer to the wav file struct created in this function
   * SIDE EFFECT: prints the information stored in the wav struct
   */
  WAV *read_file(char *wavfile)
  {
      WAV* wav_ptr = (WAV*)malloc(sizeof(WAV));
  
      FILE *fp;
      fp = fopen(wavfile,"r");
  
      fscanf (fp, "%c", wav_ptr-&gt;RIFF); //For RIFF
  
      wav_ptr-&gt;ChunkSize = little_endian_4(fp);   //For ChunkSize
  
      fscanf (fp, "%c", wav_ptr-&gt;WAVE); //For WAVE
  
      fscanf (fp, "%c", wav_ptr-&gt;fmt); //For fmt
  
      wav_ptr-&gt;Subchunk1Size = little_endian_4(fp); //for Subchunk1size
  
      wav_ptr-&gt;AudioFormat = little_endian_2(fp); //For audioformat
  
      wav_ptr-&gt;NumChan = little_endian_2(fp); //for numchan
  
      wav_ptr-&gt;SamplesPerSec = little_endian_4(fp); //for samp/sec
  
      wav_ptr-&gt;bytesPerSec = little_endian_4(fp); //for bytes/sec
  
      wav_ptr-&gt;blockAlign = little_endian_2(fp); //for block alight
  
      wav_ptr-&gt;bitsPerSample = little_endian_2(fp); //for bits/sample
  
      wav_ptr-&gt;extra = (char*)malloc((wav_ptr-&gt;Subchunk1Size - 16)*sizeof(char)); //allocate memory for extra
  
      fscanf (fp, "%c", wav_ptr-&gt;extra); //if extra
  
      fscanf (fp, "%c", wav_ptr-&gt;Subchunk2ID); // for subchunk2id
  
      wav_ptr-&gt;Subchunk2Size = little_endian_4(fp); //for subchunk 2
  
      wav_ptr-&gt;data = (short int*)malloc((wav_ptr-&gt;Subchunk2Size)*sizeof(short int)); //allocate memory for data
  
      fscanf (fp, "%hd", wav_ptr-&gt;data); //for data
  
      return wav_ptr;
  </code></pre>