Sams Teach Yourself C in 21 Days

(singke) #1
Pointers: Beyond the Basics 403

37: char buffer[80]; /* Temporary storage for each line. */ 15
38:
39: puts(“Enter one line at time; enter a blank when done.”);
40:
41: while ((n < MAXLINES) && (gets(buffer) != 0) &&
42: (buffer[0] != ‘\0’))
43: {
44: if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
45: return -1;
46: strcpy( lines[n++], buffer );
47: }
48: return n;
49:
50: } /* End of get_lines() */
51:
52: void sort(char *p[], int n)
53: {
54: int a, b;
55: char *tmp;
56:
57: for (a = 1; a < n; a++)
58: {
59: for (b = 0; b < n-1; b++)
60: {
61: if (strcmp(p[b], p[b+1]) > 0)
62: {
63: tmp = p[b];
64: p[b] = p[b+1];
65: p[b+1] = tmp;
66: }
67: }
68: }
69: }
70:
71: void print_strings(char *p[], int n)
72: {
73: int count;
74:
75: for (count = 0; count < n; count++)
76: printf(“%s\n”, p[count]);
77: }

Enter one line at time; enter a blank when done.
dog
apple
zoo
program
merry

LISTING15.7 continued

OUTPUT

25 448201x-CH15 8/13/02 11:13 AM Page 403

Free download pdf