Char array is not assigned
Ok so I want to store the word in a char array, but it gives me the 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 the 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 can just print the class name right after the string comparison, but this thing is really undermining me and I want to know why it doesn't work.
PS: Please forgive me for any obvious mistakes I may make, I only recently got into PC programming after working on uC for a couple of years.
source to share
Yes, char
arrays are not assigned in the same way as all arrays. You should use strcpy
for example
strcpy(class, "Warrior");
etc.
Also, you don't need to declare the char class[30];
long line I see in your code "undefined"
, so
char class[10];
should be ok, 9
characters "undefined"
+ 1 null terminating byte '\0'
.
And you have to prevent a buffer overflow by using scanf
this way
scanf("%1s", class);
since you only want to read the character 1
, also the comparison should be easier just
if (class[0] == 'M')
strcpy(class, "Mage");
or even thsi will 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 what scanf
really succeeded, it returns the number of arguments matched, so in your case the check would look like
if (scanf("%1s", class) == 1) ...
source to share
see this not only for character array, this is implied for all array types, but the question arises why? when we can assign another variable, why not massaging a thing: suppose:
int a[size]; a = {2,3,4,5,6};
because here the name of the array means the address of the first location of the array
printf("%p",a); // let suppose the output is 0x7fff5fbff7f0
We say that
0x7fff5fbff7f0 = something; which is not true yes we can do [0] = something, now it says to assign the array value at 0th place.
source to share