From owner-freebsd-questions Thu Feb 29 13:58:48 1996 Return-Path: owner-questions Received: (from root@localhost) by freefall.freebsd.org (8.7.3/8.7.3) id NAA13334 for questions-outgoing; Thu, 29 Feb 1996 13:58:48 -0800 (PST) Received: from lisa.rur.com ([199.202.234.53]) by freefall.freebsd.org (8.7.3/8.7.3) with SMTP id NAA13323 for ; Thu, 29 Feb 1996 13:58:43 -0800 (PST) Received: (from leo@localhost) by lisa.rur.com (8.6.11/8.6.9) id QAA08304; Thu, 29 Feb 1996 16:59:34 -0500 Date: Thu, 29 Feb 1996 16:59:33 -0500 (EST) From: Leo Papandreou To: questions@freebsd.org Subject: C question: what is wrong with this code Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-questions@freebsd.org Precedence: bulk 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); }