gpt4 book ai didi

c - 数组类型 char[] 不可赋值

转载 作者:行者123 更新时间:2023-11-30 16:56:08 25 4
gpt4 key购买 nike

这是我的第一篇文章。我一直在尝试做这个选择选择的事情,我希望用户只选择数字而不是输入它们(更容易),但是当我希望数字等于字符串时,它说“数组类型 char[30] 不可分配”。即使在后面我是否放分号。

#include <stdio.h>

int main() {
int choice1;
char word[30];

printf("You have three choice.\n");
printf("[1] Jump [2] Run [3] Dance\n");
scanf("%d",&choice1);
if (choice1 == 1)
{
word = "Jump" //Error #1
}
else if (choice1 == 2)
{
word = "Eat" //Error #2
}
else if (choice1 == 3)
{
word = "Sleep"; //Error #3
}

printf("You will now be %sing",word);

}

最佳答案

您不能分配给数组,只能复制给它。

使用strcpy相反,就像

strcpy(word, "Jump");

关于c - 数组类型 char[] 不可赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40050199/

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