title: Selection sort with linked list's
body: <p>I have the following data structure:</p>
  
  <pre><code>struct scoreentry_node {
      struct scoreentry_node *next;
      int score;
      char name[1];    
  };  
  typedef struct scoreentry_node *score_entry;
  </code></pre>
  
  <p>I am trying to create a function that consumes my structure in order and arranges them in ascending order based on the name. I want to modify the input without allocating any memory or freeing anything:</p>
  
  <p>I've tried your suggestions:</p>
  
  <pre><code>void selectionsort(score_entry *a) {
      for (; *a != NULL; *a = (*a)-&gt;next) {
          score_entry *minafteri = a;
          // find position of minimal element
          for (score_entry j = (*a)-&gt;next; j != NULL; j = j-&gt;next) {
              if (strcmp(j-&gt;name, (*minafteri)-&gt;name) == -1) {
                  *minafteri = j;
              }
          }
          // swap minimal element to front
          score_entry tmp = *a;
          a = minafteri;
          *minafteri = tmp;
      }
  }
  </code></pre>
  
  <p>I'm testing the above code with the following:</p>
  
  <pre><code>score_entry x = add(8, "bob", (add( 8 , "jill", (add (2, "alfred", NULL)))));
  iprint("",x);
  selectionsort(&amp;x);
  iprint("", x);
  clear(x); //Frees the whole list
  </code></pre>
  
  <p><code>iprint()</code> prints the score and name fields in the struct. My add function is as follows:</p>
  
  <pre><code>score_entry add(int in, char *n, score_entry en) {      
     score_entry r = malloc(sizeof(struct scoreentry_node) + strlen(n));
     r-&gt;score = in;
     strcpy(r-&gt;name, n);
     r-&gt;next = en;  
     return r;   
  }
  </code></pre>
  
  <p>I'm getting heap errors and my second print doesn't print the sorted list, it prints nothing. What am I doing wrong, and what can I do to fix it?</p>