0

I am a newbie in C and am trying to make a hangman game where a player will have to guess a random word selected by the program. But I am stuck in getting the word. I tried a lot and found some answers on SO but could not relate it to my case.

Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char dictionary[3][15] = {"food","cat","coder"};
//15 is max length of each word

char getWord() {
    srand(time(0));
    char *random_elem = dictionary[rand()%3];
    printf(random_elem);
    return random_elem;
}


void gamePlay() {
    *word = getWord();
    printf(*word);
    return;
}

int main() {

    printf("Welcome to Hangman\n");
    printf("------------------------------------\n\n");

    gamePlay();
}

The printf in the getWord() works but not in gamePlay()

The following error is generated:

<stdin>:11:12: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
    printf(random_elem);
           ^~~~~~~~~~~
<stdin>:11:12: note: treat the string as an argument to avoid this
    printf(random_elem);
           ^
           "%s", 
<stdin>:12:12: error: cannot initialize return object of type 'char' with an lvalue of type 'char *'
    return random_elem;
           ^~~~~~~~~~~
<stdin>:17:6: error: use of undeclared identifier 'word'; did you mean 'for'?
    *word = getWord();
     ^~~~
     for
<stdin>:17:6: error: expected expression
<stdin>:18:13: error: use of undeclared identifier 'word'
    printf(*word);
            ^
1 warning and 4 errors generated.

OS: Android 11 App: Cxxdroid

If that might help

3
  • getWord returns a char *, not a char. Moreover, where is word defined? Commented Jan 7, 2021 at 10:22
  • Then how shoud i write it? Commented Jan 7, 2021 at 10:27
  • I see...thank u so much...helped a lot :,-) Commented Jan 7, 2021 at 16:34

2 Answers 2

2

The right code is the above.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char dictionary[3][15] = {"food","cat","coder"};
//15 is max length of each word

char* getWord() {
    srand(time(0));
    char *random_elem = dictionary[rand()%3];
    printf("%s\n",random_elem);
    return random_elem;
}


void gamePlay() {
    char *word = getWord();
    printf("%s\n",word);
    return;
}

int main() {

    printf("Welcome to Hangman\n");
    printf("------------------------------------\n\n");

    gamePlay();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Will you please explain how char* and *word solved the issue?
Char is a character. char* is a string and you havent declared the type of variable word which is necessary.
1

In this case you can return a pointer, since the actual strings are declared with static storage duration (if they are local variables, you can't do that). You should however const-qualify the pointer since string literals are read-only. Fixed code:

const char* getWord() {
    static const char dictionary[3][15] = {"food","cat","coder"};
 
    srand(time(0));
    const char *random_elem = dictionary[rand()%3];
    puts(random_elem);
    return random_elem;
}

I moved the variable declarations inside the function since global variables should be avoided. static ensures that they still have static storage duration so you can return a pointer to them.

The warnings about "format string is not a string literal" is nothing to be concerned with in this case. If you replace printf with puts they should go away.

1 Comment

puts helps a lot but and your code solved most of the errors but still the printf i wanted did not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.