简体   繁体   中英

How to get string as return value from function in C?

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

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();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM