Programming Example - get_word (with Major Error)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
char *
get_word(void) {
int i, c;
static char word[64];
for (i = 0; i < sizeof word - 1; i++) {
c = getchar();
if (c == EOF || isspace(c))
break;
word[i] = c;
}
word[i] = '\0';
return word;
}
int
main(int argc, char *argv[]) {
char *s, *t;
printf("Enter first word: ");
s = get_word();
printf("Enter second word: ");
t = get_word(); /* ERROR: array s points to is changed */
printf("You entered '%s' and '%s'\n", s, t); /* prints second word twice */
return 0;
}
Index