title: Sorting an array of structs in C
body: <p>We were given an assignment in class, to sort array of structs. After the assignment was handed in we discussed doing the sorting by using pointers of arrays to sort it as it was more efficient than the way most people had done it.</p>
  
  <p>I decided to try and do it this way as well, however I'm running into some issues that I haven't been able to solve.</p>
  
  <p><a href="http://pastebin.com/Cs3y39yu" rel="nofollow">http://pastebin.com/Cs3y39yu</a></p>
  
  <pre><code>#include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/stat.h&gt;
  
  typedef struct stage2{//standard dec of the struct field
       char need;
           double ring;
           char fight[8];
           int32_t uncle;
           char game;
           double war;
           int8_t train;
           uint32_t beds;
           float crook;
           int32_t feast;
           int32_t rabbits;
           int32_t chin;
           int8_t ground;
           char veil;
           uint32_t flowers;
           int8_t adjustment;
           int16_t pets;
  } stage2;
  
  void usage(){//usage method to handle areas
          fprintf(stderr,"File not found\n");//prints to stderr
          exit(1);//exits the program
  }
  
  int needS(const void *v1, const void *v2)
  {
      const stage2 *p1 = v1;
      const stage2 *p2 = v2;
          printf("%c %c \n",p1-&gt;need,p2-&gt;need);
      return 0;
  }
  
  
  int main(int argc, char** argv){
          if(argc != 3){//checks for a input files, only 1
            usage();//if not runs usage
    }
    int structSize = 60;//size of structs in bytes, sizeof() doesnt return correct val
    char* fileName = argv[1];  //pull input filename
    FILE *file = fopen(fileName, "r");//opens in read mode
    fseek(file, 0, SEEK_END);  //goes to end of file
    long fileSize = ftell(file);  //saves filesize
    char *vals = malloc(fileSize);  //allocates the correct size for array based on its filesize
    fseek(file, 0, SEEK_SET);  //returns to start of file
    fread(vals, 1, fileSize, file);  //reads the file into char array vals
    fclose(file); //closes file
    int structAmount = fileSize/structSize; //determines amount of structs we need
    stage2 mainArray[structAmount]; //makes array of structs correct size
  
  
    int j;//loop variables
    int i;
  
  
    printf("need, ring, fight, uncle, game, war, train, beds, crook, feast, rabbits, chin, ground, veil, flowers, adjustment, pets\n");//prints our struct names
  
    for(i = 0; i &lt; structAmount; i ++){//initialises the array vals
          mainArray[i].need = *(&amp;vals[0+(i*60)]);
          mainArray[i].ring = *((double *)&amp;vals[1+(i*60)]);
          for(j = 0;j&lt;9;j++){
            mainArray[i].fight[j] = *(&amp;vals[j+9+(i*60)]);
          }
          mainArray[i].uncle = *((int32_t *)&amp;vals[17+(i*60)]);
          mainArray[i].game = *(&amp;vals[21+(i*60)]);
          mainArray[i].war = *((double *)&amp;vals[22+(i*60)]);
          mainArray[i].train = *((int8_t *)&amp;vals[30+(i*60)]);
          mainArray[i].beds = *((uint32_t *)&amp;vals[31+(i*60)]);   
          mainArray[i].crook = *((float *)&amp;vals[35+(i*60)]);
          mainArray[i].feast = *((int32_t *)&amp;vals[39+(i*60)]);
          mainArray[i].rabbits = *((int32_t *)&amp;vals[43+(i*60)]);
          mainArray[i].chin = *((int32_t *)&amp;vals[47+(i*60)]);
          mainArray[i].ground = *((int8_t *)&amp;vals[51+(i*60)]);
          mainArray[i].veil = *(&amp;vals[52+(i*60)]);
          mainArray[i].flowers = *((uint32_t *)&amp;vals[53+(i*60)]);
          mainArray[i].adjustment = *((int8_t *)&amp;vals[57+(i*60)]);
          mainArray[i].pets = *((int16_t *)&amp;vals[58+(i*60)]);    
    }
  
    for(i = 0; i &lt; structAmount; i ++){//prints
          printf("%c, %f, %s, %d, %c, %f, %d, %u, %f, %d, %d, %d, %d, %c, %u, %d, %d \n",
          mainArray[i].need,mainArray[i].ring,mainArray[i].fight,mainArray[i].uncle,mainArray[i].game,mainArray[i].war,mainArray[i].train,
          mainArray[i].beds,mainArray[i].crook,mainArray[i].feast,mainArray[i].rabbits,mainArray[i].chin,mainArray[i].ground,mainArray[i].veil,
          mainArray[i].flowers,mainArray[i].adjustment,mainArray[i].pets);//prints
  
    }
    free(vals);//frees the memory we allocated to vals
  
    stage2 *array = malloc(structAmount * structSize);
  
    for(i = 0; i &lt; structAmount; i ++){
          array[i] = mainArray[i];  
    }
    printf("Before Sort\n\n");  
    for(i = 0; i &lt; structAmount; i ++){
            printf("%c, %f, %s, %d, %c, %f, %d, %u, %f, %d, %d, %d, %d, %c, %u, %d, %d \n",
    array[i].need,array[i].ring,array[i].fight,array[i].uncle,array[i].game,array[i].war,array[i].train,
    array[i].beds,array[i].crook,array[i].feast,array[i].rabbits,array[i].chin,array[i].ground,array[i].veil,
    array[i].flowers,array[i].adjustment,array[i].pets);//prints 
    }
    qsort(array, structAmount,structSize,needS);
  
  
    printf("After Sort\n\n");  
    for(i = 0; i &lt; structAmount; i ++){
            printf("%c, %f, %s, %d, %c, %f, %d, %u, %f, %d, %d, %d, %d, %c, %u, %d, %d \n",
    array[i].need,array[i].ring,array[i].fight,array[i].uncle,array[i].game,array[i].war,array[i].train,
    array[i].beds,array[i].crook,array[i].feast,array[i].rabbits,array[i].chin,array[i].ground,array[i].veil,
    array[i].flowers,array[i].adjustment,array[i].pets);//prints 
    }
  
    FILE *my_file = fopen(argv[2], "wb");
    for(i = 0; i &lt; structAmount; i ++){
            fwrite(&amp;mainArray[i].need, sizeof(char), 1, my_file);
            fwrite(&amp;mainArray[i].ring, sizeof(double), 1, my_file);
            fwrite(&amp;mainArray[i].fight, sizeof(char[8]), 1, my_file);
            fwrite(&amp;mainArray[i].uncle, sizeof(int32_t), 1, my_file);
            fwrite(&amp;mainArray[i].game, sizeof(char), 1, my_file);
            fwrite(&amp;mainArray[i].war, sizeof(double), 1, my_file);
            fwrite(&amp;mainArray[i].train, sizeof(int8_t), 1, my_file);
            fwrite(&amp;mainArray[i].beds, sizeof(uint32_t), 1, my_file);
            fwrite(&amp;mainArray[i].crook, sizeof(float), 1, my_file);
            fwrite(&amp;mainArray[i].feast, sizeof(int32_t), 1, my_file);
            fwrite(&amp;mainArray[i].rabbits, sizeof(int32_t), 1, my_file);
            fwrite(&amp;mainArray[i].chin, sizeof(int32_t), 1, my_file);
            fwrite(&amp;mainArray[i].ground, sizeof(int8_t), 1, my_file);
            fwrite(&amp;mainArray[i].veil, sizeof(char), 1, my_file);
            fwrite(&amp;mainArray[i].flowers, sizeof(uint32_t), 1, my_file);
            fwrite(&amp;mainArray[i].adjustment, sizeof(int8_t), 1, my_file);
            fwrite(&amp;mainArray[i].pets, sizeof(int16_t), 1, my_file);
    }
  
    fclose(my_file);
  
    return 0;//return statement
  }
  </code></pre>
  
  <p>There is the link to the code that I have been working on. My main issue is that from what I gather, when using the sorting comparison method <code>needS</code> (line 31) for the first execution of the sort it should return the first field for the first two structs in the array and print them (I am aware that this isn't a valid sorting method but wanted to make sure the variables were what I expected). However that's not what occurs; the <code>p1</code> variable will print out what I expect however the <code>p2</code> variable will not. From this point onwards, every use of this will return junk (I think) values.</p>
  
  <p>Is there anything that I'm missing or doing wrong?</p>