gpt4 book ai didi

c++ - C、指针和void函数

转载 作者:行者123 更新时间:2023-11-30 18:22:04 25 4
gpt4 key购买 nike

已经寻找过类似的答案,但我尝试的任何方法都不起作用。有一个问题,我想通过调用 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;
}

最佳答案

字符串在 C 中不可赋值。

您需要使用strcpy()memcpy()来复制字符串

strcpy(word,randWord);

关于c++ - C、指针和void函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31197746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com