-1

同様の答えを探しましたが、私が試したことは何もありません。word問題があります。void 関数を呼び出して値を変更したいのですがinit()、単語を印刷しても機能しません。

これに何時間も費やしてきたので、助けていただければ幸いです。

int main(void)
{
   char word[MAX_WORD_LEN + 1];
   unsigned wrongGuesses = 0;
   int guessedLetters[ALPHABET_SIZE] = {
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   };
   init(&word);
   printf("%s", word);
   displayWord(word, guessedLetters);
   guessLetter(word, guessedLetters);
   return EXIT_SUCCESS;
}

void init(char* word)
{
    int randValue;
    char* randWord;
    const char* words[NUM_WORDS] = {
      "array",      "auto",       "break",      "case",       "cast",
      "character",  "comment",    "compiler",   "constant",   "continue",
      "default",    "double",     "dynamic",    "else",       "enum",
      "expression", "extern",     "file",       "float",      "function",
      "goto",       "heap",       "identifier", "library",    "linker",
      "long",       "macro",      "operand",    "operator",   "pointer",
      "prototype",  "recursion",  "register",   "return",     "short",
      "signed",     "sizeof",     "stack",      "statement",  "static",
      "string",     "struct",     "switch",     "typedef",    "union",
     "unsigned",   "variable",   "void",       "volatile",   "while"
    };
    int seed;
    seed = (time(NULL));
    srand(seed);
    randValue = rand() % NUM_WORDS;
    randWord = words[randValue];
    printf("%s", randWord);
    *word = randWord;
}
4

3 に答える 3

2

コンパイラの警告に注意してください。

word.c: At top level:
word.c:16:6: warning: conflicting types for ‘init’
 void init(char* word)
      ^
word.c:8:4: note: previous implicit declaration of ‘init’ was here
    init(&word);

init() へのポインタへのポインタを渡します。

ええ、@Gopi が言ったように、割り当てによって文字列をコピーすることはできません。

于 2015-07-03T03:26:42.823 に答える