Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a comparision function for qsort
    primarykey
    data
    text
    <p>I'm writing a comparision function that compares and organizes names by ascending order of last name and if both last names are the same, descending order of first names. The function I have right now will not do that.</p> <p>Here is the function:</p> <pre><code>int namecmp(const void *p, const void *q) { const name *pp = p; const name *qq = q; int n; if((n = strcmp(pp-&gt;last, qq-&gt;last)) != 0) return n; return qq-&gt;first - pp-&gt;first; } </code></pre> <p>I am trying to organize a dynamic array of structures, here are my structures.</p> <pre><code>typedef struct { char last[NAMESIZE]; char first[NAMESIZE]; }name; typedef struct { int id; name name; float score; }record; typedef struct { record *data; /* the dynamic array */ size_t nalloc; /* number of records allocated */ size_t nused; /* number of records in use */ } record_list; </code></pre> <p>and here is how qsort is called.</p> <pre><code>qsort(list-&gt;data, list-&gt;nused, sizeof(list-&gt;data[0]), namecmp); </code></pre> <p>Any help would be appreciated.</p> <p>EDIT: I did what you suggested now i have the wrong output.</p> <p>my output is:</p> <pre><code>3456789 Burns, Monty: 100.00 4567890 Simpson, Lisa: 95.00 1234567 Simpson, Homer: 35.50 6666666 Simpson, Bart: 45.00 2345678 Flanders, Ned: 99.50 </code></pre> <p>EDIT 2:</p> <p>How i store string into the structure.</p> <pre><code> if(sscanf(line,"%s", lastname) == 1) { if(strlen(lastname) &lt; NAMESIZE) { lastname[0] = toupper((int)lastname[0]); strcpy(rec-&gt;name.last, lastname); break; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload