char 数组不可赋值 [英] char array not assignable

查看:31
本文介绍了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? 
");
    printf("Type W for Warrior, M for Mage or R for Ranger. Then press Enter
");
    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个空终止字节''.

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

你应该用这种方式用 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");

甚至 thsi 会更具可读性

or even thsi would be more readable

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天全站免登陆