【发布时间】:2015-07-03 03:00:25
【问题描述】:
已寻找类似的答案,但我尝试的任何方法都无效。
有一个问题,我想通过调用 void 函数init() 来更改word 的值,但是当我打印单词时它不起作用。
为此花费了很多时间,因此我们将不胜感激。
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;
}
【问题讨论】:
-
您的主文件应具有以下签名:
int main(int argc, char* argv[])。特别是对于 C++(您已将此问题标记为,所以我认为这是有原因的)。 -
除了您遇到的问题之外,单词表的这种变体使得在不破坏任何东西的情况下可以更轻松地进行任性维护:
const char *words[] = {"array", "auto",..., "while"}; #define NUM_WORDS (sizeof (words)/sizeof (words[0])) -
word已经属于char*类型,因此您无需将其作为&word传递给init,因为它也是错误的!!
标签: c++ c pointers char-pointer