/* * ====================================================================== * Demonstrate use of the qsort() library function * * Copyright (C) 1994-96 by Mark Austin and David Mazzoni. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written By; D. Mazzoni and M. Austin January 1994 * ====================================================================== */ #include #include #include int main( void ) { int compare( const void *kvpArg1, const void *kvpArg2); /* Comparison function */ int iLine; /* For printing the sorted lines */ static char caaPhrases[][5] = { "four", "this", "bill", "jump", "over", "flat", "prog", "that", "dogs" }; /* [a] : Initial array contents */ printf("Initial Array Contents\n"); for( iLine = 0; iLine < sizeof(caaPhrases)/sizeof(caaPhrases[0]); ++iLine ) printf("Row %1d of caaPhrases : %s\n", iLine, caaPhrases[iLine] ); /* [b] : Call the qsort function */ qsort( (void *) caaPhrases, sizeof( caaPhrases ) / sizeof( caaPhrases[0] ), sizeof( caaPhrases[0] ), compare ); /* [c] : Print sorted lines */ printf("Sorted Array Contents\n"); for( iLine = 0; iLine < sizeof(caaPhrases)/sizeof(caaPhrases[0]); ++iLine ) printf("Row %1d of caaPhrases : %s\n", iLine, caaPhrases[iLine] ); } int compare(const void *kvpArg1, const void *kvpArg2 ) { return( strcmp((char *) kvpArg1, (char *) kvpArg2) ); }