char数组不可分配 [英] char array not assignable

查看:411
本文介绍了char数组不可分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我想将一个单词保存在char数组中,但这给我一个错误

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

这是我的代码

#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 */ 
    }
}

它给了我以下错误:

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:请原谅我可能犯的任何明显错误,在从事uC工作了几年之后,我才刚进入PC编程领域.

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.

推荐答案

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

Yes char arrays are not assignable just as all arrays aren't. You should use strcpy for example

strcpy(class, "Warrior");

以此类推.

此外,您无需声明char class[30];我在您的代码中看到的最长字符串是"undefined",所以

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

char class[10];

应该可以,"undefined"9个字符+ 1个空终止字节'\0'.

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

您应该使用scanf这样防止缓冲区溢出

And you should prevent buffer overflow with scanf this way

scanf("%1s", class);

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

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

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

甚至更具可读性

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;
}

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

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

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

这篇关于char数组不可分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆