1

I want to create a struct member haircolour with the struct variable of brown but when I try to compile I get the error "character constatnt too long for its type" and "assignment to expression with array type" I would appreciate any help on how to sort this out.

Ive tried a lot of other option such as putting double quotes around brown but this does not work

struct person {
int age;
int weight;
int height;
char haircolour[50];

};

struct person john;

john.age = 27;
john.weight = 80;
john.height = 170;
john.haircolour = 'brown';
printf("Here are the personal details of john: \n\n");
printf("age: %d \n",john.age);
printf("weight: %dkg \n",john.weight);
printf("height: %dcm \n",john.height);
printf("hair colour: %s", john.haircolour);
Trill
  • 7
  • 2

1 Answers1

2

In C, strings are written between double quotes.

john.haircolour = "brown"; //As you said, you have tried this.

The actual issue is that you are trying to copy string using assignment (=) operator. That doesn't work.

To copy, you should use strcpy OR strncpy (better.) as below.

Please read about strcpy/strncpy from here and use them.

MayurK
  • 1,925
  • 14
  • 27