rvp@softserver.canberra.edu.au (Rey Paulo) (05/09/91)
I would like to use qsort to sort an array of strings. According to the manual of qsort, the parameters that must be supplied are base, nel, width, and compare. My problem is that width which is the size of each element in the array, varies with each string element. What value of width should I pass to qsort to make it do the job. My data structure is char *filenames[], and I would like qsort to sort the array. Any comment appreciated. -- Rey V. Paulo | Internet: rvp@csc.canberra.edu.au University of Canberra | I am not bound to please thee with my answer. AUSTRALIA | -Shylock, in "The Merchant of Venice" ------------------------------+----------------------------------------------
darcy@druid.uucp (D'Arcy J.M. Cain) (05/09/91)
In article <1991May9.042556.5565@csc.canberra.edu.au> Rey Paulo writes: >and compare. My problem is that width which is the size of each element in >the array, varies with each string element. What value of width should I >pass to qsort to make it do the job. My data structure is char *filenames[], >and I would like qsort to sort the array. Actually the size of each element is exactly the same throughout the array. It is the size of a pointer to character and the size of the string has no bearing on the case. Use something like: qsort(filenames, number_of_filenames, sizeof(filenames[0]), sort_routine); Hey, here's a switch. This is a C language specific question, not an OS related one. Followups to comp.lang.c. -- D'Arcy J.M. Cain (darcy@druid) | D'Arcy Cain Consulting | There's no government Toronto, Ontario, Canada | like no government! +1 416 424 2871 |
jik@athena.mit.edu (Jonathan I. Kamens) (05/09/91)
When you pass an array of strings into qsort, the width of each string in the array does *not* vary according to its length. Remember, literally, what char *filenames[] means. It means an array of pointers to char, not an array of strings. And pointer to char, or char *, type, is of a constant size. Therefore, you would use: qsort((char *) filenames, num_names, sizeof(char *), cmp); Note that the "cmp" function *cannot* be strcmp, since strcmp expects two char *'s, and what it's actually going to get is to char **'s (read the man page for qsort carefully -- it says that the comparison function gets two pointers to elements of the array, and remember that the elements of the arrays are char *'s). The sample program appended to the end of this message illustrates all of this. -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710 -- #include <stdio.h> #include <strings.h> char *filenames[] = { "The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dogs." }; print_names() { int i; for (i = 0; i < sizeof(filenames) / sizeof(char *); i++) printf("%s ", filenames[i]); printf("\n"); } cmp(char **str1, char **str2) { return(strcmp(*str1, *str2)); } main() { print_names(); qsort((char *) filenames, sizeof(filenames) / sizeof(char *), sizeof(char *), cmp); print_names(); }
jimr@hplsdv7.COS.HP.COM (Jim Rogers) (05/10/91)
Since your data structure is char *filename[] you should pass sizeof(filename[0]) as the size of your element. Your strings may differ in size, but the arrays of characters which hold your strings are all the same size. This size is the "element" size required by qsort. Remember that a string in C is an array of characters with a '\0' somewhere in that array. The position of the '\0' determines the length of the string but not the size of the array. Jim Rogers Hewlett-Packard Company Colorado Springs, Colorado United States of America