1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define SIZE 40 char * * mal_ar(int n); int main(void) { int words, i; char * * st; printf("How many words do you wish to enter? "); scanf("%d", &words); getchar(); printf("Enter %d words now:\n", words); st = mal_ar(words); printf("Here are your words:\n"); for (i = 0; i < words; i++) { puts(st[i]); free(st[i]); } free(st);
return 0; }
char * * mal_ar(int n) { char * * pt; int i, j; char ch;
pt = (char * *)malloc(n * sizeof(char *)); for (i = 0; i < n; i++) { pt[i] = (char *)malloc(SIZE * sizeof(char)); while (isspace(ch = getchar())) continue; pt[i][0] = ch; j = 1; while (!isspace(pt[i][j] = getchar())) j++; pt[i][j] = '\0'; }
return pt; }
|