gpt4 book ai didi

c - 通过函数传递字符串。 C

转载 作者:行者123 更新时间:2023-12-04 08:18:01 27 4
gpt4 key购买 nike

<分区>

void printInstructions();
char *getUserWord();


int main()
{
printInstructions();
char *baseWord = getUserWord();
printf("%s", baseWord);
return 0;
}


void printInstructions()
{
printf(" Instructions: \n"
"===================================================================\n"
"= This program is a hangman game. =\n"
"= The first user will enter the name to be guessed =\n"
"= After that, the second user will guess the letters of the word =\n"
"= the second user will loose if they have three strikes =\n"
"===================================================================\n");
return;
}


char *getUserWord()
{
static char str[20];
scanf("%s", str);
return str;
}

我的函数 getUserWord 通过传递一个字符串来完成它的任务。根据我在网上阅读的内容,char *baseWord = getUserWord(); 是将字符串分配给主函数中的变量的唯一方法。我不知道为什么会这样,我什至不确定这在内存中做了什么。以下是对我有意义的内容。为什么这行不通?

 char baseWord[21];
baseWord = getUserWord();

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