when I cannot figure out the issue with my code. The goal of the program is to read words from a text file and add them to a linked list. When I run this:
static char *make_string(char buffer[], int length) {
char *str = (char *)(malloc(length+1));
memcpy(str, buffer, length);
str[length + 1] = '\0';
return str;
}
char *words_next_word() {
char buf[MAXBUF] = {0};
int character = getchar();
int index = 0;
static int count = 0;
printf("it is the %d word \n", count);
count++;
while(isalnum(character) == 0){
character = getchar();
}
while(character != EOF && isalnum(character) != 0){
buf[index] = character;
index++;
character = getchar();
}
return make_string(buf, index);
}
After getting the word from the text file, I add it to a linked list. After adding the word, I free the string. The first 138 words are read and freed without issue. For some reason, the program crashes when trying to read the 138'th word, but I don't know why.