title: sorting structs in C with pointers
body: <p>I am just starting in C and have very little clue as to what's going on behind the scenes. I'm learning it on the fly for a data structures class which makes things a bit harder. </p>
  
  <p>Update: I've stripped the program back down and am starting with memory and on up. I have the allocate and deallocate functions in there and I'm getting a malloc error: Q1(9882) malloc: <strong>* error for object 0x7fff59daec08: pointer being freed was not allocated
  *</strong> set a breakpoint in malloc_error_break to debug</p>
  
  <p>Update2 here's my revised code, it's still missing something, a couple of my printf statements aren't showing up:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;math.h&gt;
  #include&lt;assert.h&gt;
  
  static int size = 10;
  
  struct student{
      int id;
      int score;
  };
  
  struct student* allocate(){
       /*Allocate memory for ten students*/
       struct student *s = malloc(size*(sizeof(struct student)));
       assert(s != 0);
       /*return the pointer*/
       return s;
  }
  
  void generate(struct student* students){
      /*Generate random ID and scores for ten students, ID being between 1 and 10, scores between 0 and 100*/
      srand((unsigned int)time(NULL));
      int id[size];
      int y;
  
      for (int i = 0; i &lt; size; i++){
          y = rand() % size + 1;
          while(dupe(id, i, y)){
              y = rand() % size + 1;
          }
          id[i] = y;
      }
  
      for (int j = 0; j &lt; size; j++){
          (students + j)-&gt;id = id[j];
          (students + j)-&gt;score = rand() % 101;
          printf("ID: %d\tScore: %d\n", (students + j)-&gt;id, (students + j)-&gt;score);
      }
  }
  
  int dupe(int id[], int size1, int i){
      for (int x = 0; x &lt; size1; x++){
          if(id[x] == i)
              return 1;
      }
      return 0;
  }
  
  void output(struct student* students){
       /*Output information about the ten students in the format:
                ID1 Score1
                ID2 score2
                ID3 score3
                ...
                ID10 score10*/
      sort(&amp;students);
      for(int x = 0; x &lt; size; x++){
          printf("ID: %d\tScore: %d\n", (students + x)-&gt;id, (students + x)-&gt;score); //print stmt not showing
      }
  }
  
  void sort(struct student* students){
      struct student *sd = allocate();
  
      struct student *stud;
  
      for(int i = 0; i &lt; size; i++){
          stud = &amp;students[i];
          sd[stud-&gt;id] = *stud;
      }
      for(int x = 0; x &lt; size; x++){
          printf("ID: %d\tScore: %d\n", (sd + x)-&gt;id, (sd + x)-&gt;score); //print stmt not showing
      }
      students = &amp;sd;
      deallocate(sd);
  }
  
  void summary(struct student* students){
       /*Compute and print the minimum, maximum and average scores of the ten students*/
  
  }
  
  void deallocate(struct student* stud){
       /*Deallocate memory from stud*/
      free(stud);
  }
  
  int main(){
      struct student* stud = NULL;
      char c[] = "------------------------------\n";
      /*call allocate*/
      stud = allocate();
      /*call generate*/
      generate(&amp;stud);
      /*call output*/
      printf("%s", c);
      output(&amp;stud);
      /*call summary*/
  
      /*call deallocate*/
      deallocate(stud);
  
      return 0;
  }
  </code></pre>