Sort Linked List alphabetically
<p>I'm trying to sort my list alphabetically but i'm having some issues. I have the following:</p>

<pre><code>    struct listNodeData
        {
            int value;
            char *position;
            char *lastName;
            struct listNodeData * next;
        };
        typedef struct listNodeData listNodeData;

void sortList(listNodeData *List)
{
    int swapped;
    listNodeData *ptr1;
    listNodeData *lptr = NULL;

    do
    {
        swapped = 0;
        ptr1 = List-&gt;next;

        while (ptr1-&gt;next != lptr)
        {
            if (strcmp(ptr1-&gt;lastName,ptr1-&gt;next-&gt;lastName) &gt; 0)
            {
                swap(ptr1,ptr1-&gt;next);
                swapped = 1;
            }
            ptr1 = ptr1-&gt;next;
        }
        lptr = ptr1;
    }
    while (swapped);

}

void swap (listNodeData *a,listNodeData *b)
{

    char *lastName = malloc(sizeof(char) * 20);
    strcpy(lastName,a-&gt;lastName);
    strcpy(a-&gt;lastName,b-&gt;lastName);
    strcpy(b-&gt;lastName,lastName);

}
</code></pre>

<p>I am trying to sort the linked list by last name. Right now i'm just trying to swap the last names of my nodes before worrying about swapping the value and position along with the last name. When I compile my program, it works when I run it I get Bus error 10.</p>

<p>I'm not exactly sure why im getting bus error. I've looked at it, and it might have something to do with my strcmp? I'm not sure why because i malloced lastName when I initialized my node, and am mallocing inside the swap function.</p>

<p>The linked list has a dummy header node, so thats why I have 
<code>ptr1 = List-&gt;next</code> instead of <code>ptr1 = List</code></p>

<pre><code>listNodeData *initNode(int number,char *lastName,char *position)
{
    listNodeData *newNode;
    newNode = malloc(sizeof(listNodeData));
    newNode-&gt;position = malloc(sizeof(char) * 20);
    newNode-&gt;position = position;
    newNode-&gt;lastName = malloc(sizeof(char) * 20);
    newNode-&gt;lastName = lastName;
    newNode-&gt;value = value;
    newNode-&gt;next = NULL;
    return (newNode);
}
</code></pre>