Goofing around with qsort in C

plus making the compiler flags more angry.
This commit is contained in:
Dan Buch 2012-05-29 15:01:12 -04:00
parent 86702165fe
commit b3cf0c7c5b
2 changed files with 36 additions and 2 deletions

View File

@ -1,8 +1,8 @@
CFLAGS += -g -Wall -Wextra CFLAGS += -g -Wall -Wextra -pedantic -pedantic-errors
%.class:%.java %.class:%.java
javac -d $(PWD) $^ javac -d $(PWD) $^
all: temperatures URLParts.class all: temperatures qsort-example URLParts.class
.PHONY: all .PHONY: all

View File

@ -0,0 +1,34 @@
/*
* C Sorting example mostly from qsort(3)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
static int cmpstringp(const void *p1, const void *p2) {
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(*(char * const *)p1, *(char * const *)p2);
}
int main(int argc, char *argv[]) {
int j;
if (argc < 2) {
fprintf(stderr, "Usage: %s <string>...\n", basename(argv[0]));
exit(EXIT_FAILURE);
}
qsort(&argv[1], argc - 1, sizeof(argv[1]), cmpstringp);
for (j = 1; j < argc; j++) {
puts(argv[j]);
}
exit(EXIT_SUCCESS);
}