From owner-freebsd-questions Thu Feb 29 15:33:24 1996 Return-Path: owner-questions Received: (from root@localhost) by freefall.freebsd.org (8.7.3/8.7.3) id PAA19614 for questions-outgoing; Thu, 29 Feb 1996 15:33:24 -0800 (PST) Received: from lisa.rur.com (G338.257.InterLink.NET [199.202.234.53]) by freefall.freebsd.org (8.7.3/8.7.3) with SMTP id PAA19596 for ; Thu, 29 Feb 1996 15:33:17 -0800 (PST) Received: (from leo@localhost) by lisa.rur.com (8.6.11/8.6.9) id SAA08829; Thu, 29 Feb 1996 18:34:17 -0500 Date: Thu, 29 Feb 1996 18:34:17 -0500 (EST) From: Leo Papandreou To: questions@FreeBSD.org Subject: Re: C question: what is wrong with this code In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-questions@FreeBSD.org Precedence: bulk Never mind. int keycmp(const void *a, const void *b) { return strcmp(*((char **)a), *((char **)b)); } and in main() ... qsort(table, n, sizeof(char *), keycmp); ... does the trick. On Thu, 29 Feb 1996, Leo Papandreou wrote: > > > I am having a problem with elementary C. The following stub > reproduces the problem exactly. What am I doing wrong? > > > #include > #include > #include > > #define TABLESIZE 10 > #define ITERATIONS 10 > > main() > { > > int i, j , n; > char *table[TABLESIZE], buff[1024]; > > for (i = 0; i < ITERATIONS; i++) > { > /* > * populate the table with some random strings > */ > n = random() % TABLESIZE + 1; > for (j = 0; j < n; j++) > { > sprintf(buff, "%d", random() % 10000); > table[j] = strdup(buff); > } > > for (j = 0; j < n; j++) > printf("%s\t", table[j]); > printf("\n"); > > /* so far, so good. Now sort the table */ > qsort(table, n, sizeof(char *), strcmp); > for (j = 0; j < n; j++) > printf("%s\t", table[j]); > printf("\n"); > /* huh!? Doesnt look sorted, does it? */ > > for (j = 0; j < n; j++) > free(table[j]); > } > > exit(0); > } >