usercfg usercfg - 1 year ago 44
C Question

Error while passing value across 2 functions in C

I'm trying to pass the value of variable

char mov_name[51]
which is in the startscreen function to my main function, and the variables will be outputted as string later on. The program runs, however the output is not what it should be. Any help would be much appreciated!

void startscreen(char mov_name[51],char date_select[51]);

void startscreen(char mov_name[51],char date_select[51])
scanf("%s", &mov_name);

scanf("%s", &date_select);

void main()
char mov_name[51], date_select[51];
startscreen(mov_name ,date_select);

printf("%s %s", mov_name, date_select);


enter image description here

Answer Source

The function int scanf(const char *format, ...) reads input from stdin in the specified format (documentation here). The problem is your are passing a char ** instead of a char * to scanf. mov_name and date_select have type char *. The ampersand & takes the address of move_name and data_select, resulting in a char **.