' char* strInput =(char*) malloc(sizeof(char));
        int ch;
        int letNum = 0;
        while((ch = getchar()) != EOF){
                letNum++;
                strInput = (char*)realloc(strInput,letNum*sizeof(char));
                *(strInput + letNum - 1) = ch;
        }
        printf("\n");
        printf("%s\n",strInput);
        free(strInput);`

This is the contents of main in a program I wrote that takes an undefined number of chars and prints the final string. I don’t understand why but it only works if I press ctrl+D twice, and only once if I press enter before.

does anyone get what’s going on? And how would you have written the program?

  • rastignac@programming.devOP
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    Thank you, I realize that there’s a whole other aspect I didn’t even consider. I’m new to C and Linux so I’ll follow your advice but it’s making me want to learn more. Thanks again to both you and @adriator for your answers