I am writing a phonebook application in C. I have an array (sort2
) of structure Nodes (contacts). I am trying to sort the array by nodes->pnum
(phone number); s
is the number of nodes in array. For some reason, the loop creates an error after first iteration. Why?
for(i=0; i<s;i++)
{
for(j=0;j<s;j++)
{
num1= sort2[i];
num2= sort2[j];
if(num1->pnum<num2->pnum)
{
temp=sort2[j];
sort2[j]=sort2[j+1];
sort2[j+1]=temp;
printf("%s",sort2[j]->lname);
printf("%s",sort2[j+1]->lname);
}
}
}
qsort
is for. Anyway, you'd better hopesort2
has more thans
elements. – chris 21 hours ago