Sorting Array of Structures

A

Allie

Richard said:
Incidentally, you have misquoted me. I most certainly did not write:

sizeof( book ) / sizeof ( book[0] )

Your original qsort( book, sizeof book / sizeof book[0], sizeof
book[0], comptitle ); did not work... so I had to adjust it.

This is what I get as output when I use your qsort:

Inventory of books printed in alphabetical order by title:

AUTHOR TITLE CODE #COPY #BORR #AVAIL
Galvin Operating Systems G02 15 2 13
Golumbic Graph Theory G01 5 2 3
Habermann Operating Systems H01 10 5 5
Herzog Computing Structures H03 10 7 3
Holub Compiler Design H05 11 8 3
Horowitz Programming Languages H06 16 10 6
Hughs Structured Programming H02 4 4 0
Hunter Understanding C H04 6 6 0
Jacobs Database Logic J01 3 1 2
Kanter Management Information K01 8 1 7
Kindred Data Systems K04 2 0 2
Kleinrock Queueing Systems K03 14 0 14
Kuo Numerical Methods K02 2 0 2
Lane Data Communications L01 20 3 17
Mano Computer Architecture M01 2 2 0
Schroder C S01 7 2 5

The books are listed alphabetically by author because that's what the
previous function (using qsort( book, n, sizeof( book[0] ), compauthor
);) did. sortbytitle() didn't do anything to the library.
 
R

Richard Heathfield

Allie said:
Richard said:
Incidentally, you have misquoted me. I most certainly did not write:

sizeof( book ) / sizeof ( book[0] )

Your original qsort( book, sizeof book / sizeof book[0], sizeof
book[0], comptitle );

That looks more like it.
did not work... so I had to adjust it.

Then you have used it in a different context to that in which I posted it,
which was in the context of an array at file scope. What may have happened
is that you have decided to use a function to wrap around the qsort (which
is fine), and then decided to send the book array in as a parameter. In
that context, it would be converted to a pointer to the first element in
that book array, and the sizeof book / sizeof book[0] calculation would
break as a result.

In that new context, you would indeed have to supply the number of elements
in the array "by hand", as you now seem to have done.

The books are listed alphabetically by author because that's what the
previous function (using qsort( book, n, sizeof( book[0] ), compauthor
);) did. sortbytitle() didn't do anything to the library.
^^^^^^^^^^^

Aha! My hypothesis appears to be correct.
 
A

Allie

Richard said:
[...] What may have happened
is that you have decided to use a function to wrap around the qsort (which
is fine), and then decided to send the book array in as a parameter. In
that context, it would be converted to a pointer to the first element in
that book array, and the sizeof book / sizeof book[0] calculation would
break as a result.

In that new context, you would indeed have to supply the number of elements
in the array "by hand", as you now seem to have done.

That is what happened, yes. Once again, thank you :)

--Allie
 
K

Keith Thompson

Richard Heathfield said:
Allie said:
To Richard: qsort( book, n, sizeof( book[0] ), comptitle ); gives me
the right output. You had written qsort( book, sizeof( book ) / sizeof
(book[0] ), sizeof book[0], comptitle );

What I wrote was based on the information you gave me. If you gave me the
wrong information, that is your lookout, not mine.

Quoting the article that started this thread:

] How would I go about sorting this structure by title?
]
] typedef struct {
] char author[40];
] char title[40];
] char code[4];
] int hold;
] int loan;
] } LIBRARY;
]
] LIBRARY book[N];
]
]
] This is what I've written:
]
] void sortbytitle( LIBRARY *b, int n ) { /* n = number of books in stock
] */
] LIBRARY temp[N];
] int i;
]
] for( i = 0; i < n; i++ ) {
] if( strcmp( b.title, b[i + 1].title ) > 0 ) {
] temp = b;
] b = b[i + 1];
] b[i + 1] = temp;
] }
] }
]
] return;
] }

The code is wrong in a number of ways (the sorting algorithm doesn't
work, and the use of an array of temporaries is unnecessary), but
given the declarations of book (an array of N LIBRARY objects) and
sortbytitle (a function taking a pointer to LIBRARY and an int n,
"number of books in stock"), it seemed reasonably obvious from the
beginning that N is the total size of the array and n is the number of
array elements that contain valid values.

The use of both N and n as identifiers is legal, but confusingly poor
style.

Richard, I'm not surprised that you missed this, but it was there.
 
R

Richard Heathfield

Keith Thompson said:
Richard, I'm not surprised that you missed this, but it was there.

Fair enough. In fact I took one quick look at his code and thought "blech,
let's crack out a qsort ASAP!" so I'm not surprised I missed it, either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top