0

I want to get many string inputs with "gets" ("Enter" should be considered as input in my program). but my program always crushes after the first "gets" input This is a simple example that could probably explain what I am talking about:

int main()
{
    char* str;
    char* str1;
    puts("Enter the first String");
    gets(str);
    fflush(stdin);
    puts(str);
    puts("Enter the second String");
    gets(str1);
    puts(str1);
    return 0;
}

the program crushes just before getting the second string input

Wadi3
  • 1
  • 3
  • You must allocate some buffer to read the strings in before reading strings. Also you shouldn't use `gets()`, which has unavoidable risk of buffer overrun, deprecated in C99 and removed from C11. Another note is that `fflush(stdin);` [invokes *undefined behavior*](https://stackoverflow.com/questions/2979209/using-fflushstdin), so you shouldn't use that. – MikeCAT May 04 '21 at 12:57
  • 1
    `char* str` -> `char str[100]`, read the chapter dealing with strings in your C text book – Jabberwocky May 04 '21 at 12:58

0 Answers0