7

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.

Luxuspunch
  • 95
  • 1
  • 1
  • 3

4 Answers4

16

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

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[10];

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

And you should prevent buffer overflow with scanf this way

scanf("%1s", class);

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

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

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

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) ...
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • @Luxuspunch see some suggestions to improve your code. – Iharob Al Asimi Jan 09 '15 at 19:11
  • @Luxuspunch if you think any of the answers helped you, you can accept the answer by clicking the check mark see [this](http://stackoverflow.com/tour). May be you already know this, but in case you don't... – Iharob Al Asimi Jan 09 '15 at 19:20
  • Sorry I was eating. Anyways, thanks A LOT to everyone contributing here. Always makes my day when someone can help me or vice versa :) Also I do appreciate your improvements. It was an early version as stated in my post but I didn't notice that I could clean it up so much. – Luxuspunch Jan 09 '15 at 19:42
6

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.

Anurag Bhakuni
  • 2,379
  • 26
  • 32
1
 class = "Ranger";

should be

strcpy(class,"Ranger");

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

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • Yep, thank you too for answering. Strangely I have never found this anywhere for about 1 hour of research. I'm really starting to doubt my googling skills. I will mark this question as solved as soon as I can – Luxuspunch Jan 09 '15 at 19:08
  • @Luxuspunch no probs but this is basics of dealing with strings in c. – Gopi Jan 09 '15 at 19:09
  • Well now I know. On the uC's I programmed we did never work with strings, that may be the reason I'm not too familiar with this. – Luxuspunch Jan 09 '15 at 19:12
  • @Luxuspunch it's not easy to google for this things, but I assure you many similar questions are asked every day her on SO, i've ansewered a lot of questions where the problem was exactly this one. – Iharob Al Asimi Jan 09 '15 at 19:17
0

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

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

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

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

and it should work !