0

I should mention that I am on Turbo C++ (yes the old one) because it is required by my school.

I have a structure like this:

struct move{
 int power;
 int pp;
 char name[10];
 };

When I try to make a new variable if I do this:

move tackle;
tackle.pp = 10;
tackle.power = 20;
tackle.name = "tackle";

I get an error as:

Error NONAME00.CPP 11: Lvalue required

But this works:

move tackle = {20, 10, "tackle"}

it works.

What am I doing wrong?

P.S. line 11 is the tackle.name = "tackle", sorry if I was unclear earlier.

poiasd
  • 149
  • 2
  • 13

3 Answers3

3

You can't assign to an array, but you can initialise it.

tackle.name = "tackle";

is an assignment, while

move tackle = {20, 10, "tackle"};

is an initialisation.

To replace the contents of the array, use strcpy:

strcpy(tackle.name, "tackle");

or, better, use string if you're allowed to:

#include <string>

struct move{
 int power;
 int pp;
 string name;
 };
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
2

You are using a c-style string and you should initialize it via

strcpy(tackle.name, "tackle");
marom
  • 5,064
  • 10
  • 14
0

This line

tackle.name = "tackle";

tries to assign one array to another which not a supported operation in C or C++.

However initialization like char name[10] = "tackle" is fine by the standard and is supported.

Alexander Balabin
  • 2,055
  • 11
  • 13