Once I'd tidied up a little, and fixed the (modern compiler enforced) errors, it works fine:
#include <iostream>
char *get_word(const char *player_A , const char *player_B);
int main(void)
{
const char *A = "Player_1";
const char *B = "Player_2";
char *word_A;
char *word_B;
word_A = get_word(A,B);
printf("Word_A => %s\n" , word_A);
word_B = get_word(B,A);
printf("Word_B => %s\n" , word_B);
}
char *get_word(const char *player_A , const char *player_B)
{
char *word = (char*) malloc(50);
printf("%s give a word without %s looking at it: " , player_A , player_B);
scanf("%s" , word);
return word;
}
The thing to note is that
scanf
terminates input on whitespace, not just newline:
C library function - scanf() - Tutorialspoint[
^] - so "hello world" will be read as two inputs: "hello" and "world".
If you want to read the whole line, you will need to use
C library function - fgets() - Tutorialspoint[
^] instead.