Search code examples
ccommand-line-argumentsc-strings

C user dynamic input with argv


So I try to take a user input of unknown max length from the command line. The program to be able to do something like that needs to take a dynamic input of a string (char array maybe?). The outcome should be something like

./a.out st1 your str is ...

the only code I was able to come up with is the following

int main (int argc, char* argv[]){
    ...
    char str1[];
    str1 = argv[1];
    printf("your...);
    ...
}

Solution

  • This declaration of a block scope array with an empty number of elements:

    char str1[];
    

    is incorrect. Moreover arrays do not have the assignment operator:

    str1 = argv[1];
    

    You could declare a pointer:

    char *str1;
    str1 = argv[1];
    printf("your...);