I have been sitting on this simple fundamental mistake for a while.
How do we avoid strings in the stdin, that are bigger than a defined sizeof(sring). Here sizeof(stdin_passed) > sizeof(word_new). When we are restricted to use ferror or feof.
Thanks
int main() {
int word_count= 0;
char **word_ARRAY= malloc(sizeof(char*));
if (word_ARRAY== NULL) {
perror("malloc");
exit(1);
}
char word_new[42];
while(1) {
/*help needed here: ouput is Segmentation Error*/
if (fgets(word_new, sizeof(word_new), stdin) == NULL){
// error occurs, thus use perror
if(ferror(stdin)){
perror("error occured in stdin");
exit(1);
}
// no error and only EOF reached
break;
}
New contributor
plantpot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.