I've come across such an example of getword. I understand all the checks and etc. but I have a problem with ungetc.
When the c does satisfy if ((!isalpha(c)) || c == EOF)and also doesn't satisfy while (isalnum(c)) -> it isn't a letter, nor a number - ungetc rejects that char.
Let's suppose it is '\n'.
Then it gets to return word however it can't be returned since it is not saved in any array. What happens then?
while (isalnum(c)) {
if (cur >= size) {
size += buf;
word = realloc(word, sizeof(char) * size);
}
word[cur] = c;
cur++;
c = fgetc(fp);
}
if ((!isalpha(c)) || c == EOF) {
ungetc(c, fp);
}
return word;
EDIT @Mark Byers - thanks, but that c was rejected for a purpose, and will not satisfy the condition again and again in an infinite loop?
getwordfunction, not agetwordahdonemorecharacterwhateveritmaybe. It reads until it encounters a character that isn't alphanumeric. It then puts that character back into the stream and returns. Presumably it returns a character pointer, but you omitted the function declaration so I'm not 100% sure. The!isalpha(c)in the if statement after the loop is equivalent to true, because the character will never be alphabetic (if it was, the loop wouldn't have broken). Unless the loop is capable of breaking during error handling.