C语言 字符数组不可分配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27867437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:41:13  来源:igfitidea点击:

char array not assignable

carrayschar

提问by Luxuspunch

Okay, so I want to save a word in a char array but it gives me a error

好的,所以我想在字符数组中保存一个单词,但它给了我一个错误

Here's my code

这是我的代码

#include <stdio.h>
#include <time.h> 
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char yesno[30] = "n";        //yes/no answer
    char class[30] = "undefined";//choosen class 
    int classchoosen = 0;        

    /* initialize random seed: */
    srand ( time(NULL) );

    printf("Welcome, which class do you wanna play with? \n");
    printf("Type W for Warrior, M for Mage or R for Ranger. Then press Enter\n");
    while(classchoosen == 0)
    {
        scanf("%s", class);
        if(strcmp(class, "W") == 0)
        {
            classchoosen = 1; 
            class = "Warrior";
        }
        if(strcmp(class, "M") == 0)
        {
            classchoosen = 1; 
            class = "Mage";
        }
        if(strcmp(class, "R") == 0)
        {
            classchoosen = 1; 
            class = "Ranger";
        }
        if(classchoosen == 0)
        {
            class = "undefined";
        }
        printf("So you wanna play as a %s? Enter y/n", class);
        classchoosen = 0; //For testing, remove later 

    }
    while(1)
    {
        /* Irrelevant stuff */ 
    }
}

And it gives me following errors:

它给了我以下错误:

damagecalc.c:44:13: error: expected identifier
                        class -> "warrior";
                                 ^
damagecalc.c:49:10: error: array type 'char [30]' is not assignable
                        class = "mage";
                        ~~~~~ ^
damagecalc.c:54:10: error: array type 'char [30]' is not assignable
                        class = "ranger";
                        ~~~~~ ^
damagecalc.c:58:10: error: array type 'char [30]' is not assignable
                        class = "warlock";
                        ~~~~~ ^
4 errors generated.

I know I could just print out the class name just after the string comparison, but this thing is really bugging me and I want to know why it doesn't work.

我知道我可以在字符串比较之后打印出类名,但是这件事真的让我烦恼,我想知道为什么它不起作用。

PS: Please forgive me for any obvious mistakes I may do, I just got into PC programming recently after having worked on uC's for a couple of years.

PS:请原谅我可能犯的任何明显错误,我在 uC 上工作了几年后最近才进入 PC 编程。

回答by Iharob Al Asimi

Yes chararrays are not assignable just as all arrays aren't. You should use strcpyfor example

是的,char数组不可分配,就像所有数组都不可分配一样。你应该使用strcpy例如

strcpy(class, "Warrior");

and so on.

等等。

Also, you don't need to declare char class[30];the longest string I see in your code is "undefined"so

此外,您不需要声明char class[30];中最长的字符串我看到你的代码是"undefined"这样

char class[10];

should be ok, 9characters of "undefined"+ 1 null terminating byte '\0'.

应该没问题9"undefined"+ 1 个空终止字节的字符'\0'

And you should prevent buffer overflow with scanfthis way

你应该用scanf这种方式防止缓冲区溢出

scanf("%1s", class);

since you only want to read 1character, also the comparison should be simpler just

由于您只想读取1字符,因此比较应该更简单

if (class[0] == 'M')
    strcpy(class, "Mage");

or even thsi would be more readable

甚至thsi会更具可读性

classchosen = 1;
switch (class[0])
{
case 'M':
    strcpy(class, "Mage");
    break;
case 'R':
    strcpy(class, "Ranger");
    break;
case 'W':
    strcpy(class, "Warrior");
    break;
default:
    classchosen = 0;
}

and finally check that scanfactually succeeded, it returns the number of arguments matched so in your case a check would be like

最后检查是否scanf实际成功,它返回匹配的参数数量,因此在您的情况下,检查将类似于

if (scanf("%1s", class) == 1) ...

回答by Anurag Bhakuni

see this is not only for character array , this implies for all type of arrays but the question aries why?,when we can assign other variable why not array the thing is:- suppose we have:

看到这不仅适用于字符数组,这意味着适用于所有类型的数组,但问题是为什么?,当我们可以分配其他变量时,为什么不是数组,事情是:-假设我们有:

int a[size];
a = {2,3,4,5,6};

because here the name of an array mean address of the first location of an array

因为这里的数组名是指数组第一个位置的地址

printf("%p",a); // let suppose the output is 0x7fff5fbff7f0

We are saying by that

我们是这么说的

0x7fff5fbff7f0 = something; which is not correct yes we can do a[0] = something , now it saying assign the value of array at 0th location.

0x7fff5fbff7f0 = 某物;这是不正确的,我们可以做 a[0] = something ,现在它说在第 0 个位置分配数组的值。

回答by Gopi

 class = "Ranger";

should be

应该

strcpy(class,"Ranger");

Fix the same in all the places. char arrays are not assignable

在所有地方修复相同。字符数组不可分配

回答by Meninx - メネンックス

switch class = "Warrior";to strcpy(class, "warrior");

切换class = "Warrior";strcpy(class, "warrior");

switch class = "Mage";to strcpy(class, "Mage");

切换class = "Mage";strcpy(class, "Mage");

switch class = "Ranger";to strcpy(class, "Ranger");

切换class = "Ranger";strcpy(class, "Ranger");

switch class = "undefined";to strcpy(class, "undefined");

切换class = "undefined";strcpy(class, "undefined");

and it should work !

它应该工作!