Need assistance with a dynamic structure assignment of a char type value

Hello,
I am attempting to use a dynamic array with a structure to assign a string to a value in the structure (I appologize for the bad wording, I haven't been programing in C++ for very long), however, I get an error when I try to compile the program.
The error(s) are:
Lvalue required in function main()
Pointer to structure required on left side of -> or ->* in function main()
and again:
Lvalue required in function main()

The code that the errors refers to is:

candyBar * ps = new candyBar[3];
ps[0].brand = "Hersheys";
ps[1]->brand = "Snickers";
ps[2].brand = "M&Ms";

I declared my structure as follows:

struct candyBar
{
char brand[];
float weight;
int calories;
};

I am using Borland C++ 5.5.1, it is a compiler only, there is no IDE.

Any help would be very appreciated, and thanks in advance for a quick reply.
closed account (z05DSL3A)
ps[1]->brand = "Snickers"; should be ps[1].brand = "Snickers";

I would also expect your compiler to error on char brand[]; along the lines of illegal zero-sized array.
Last edited on
That gives me the same error as the other two:
Lvalue required in function main()
If it would help, I can post my entire program, those are only parts of it, by the compiler says that everything else is fine.
Last edited on
closed account (z05DSL3A)
It would probably help if you posted more code (use the # button to format it).

I'm about to head off home, someone else may help further or I'll look at it later on (in a few hours).
Ok, here is the entire program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
struct candyBar
{
  char brand[];
  float weight;
  int calories;
};
int main()
{
  candyBar * ps = new candyBar[3];
  ps[0].brand = "Hersheys";
  ps[0].weight = 3.2;
  ps[0].calories = 23;
  cout << "Brand: " << ps[0].brand;
  cout << "\nWeight: " << ps[0].weight;
  cout << "\nCalories: " << ps[0].calories << "\n";
  ps[1].brand = "Snickers";
  ps[1].weight = 4.0;
  ps[1].calories = 35;
  cout << "\nBrand: " << ps[1].brand;
  cout << "\nWeight: " << ps[1].weight;
  cout << "\nCalories: " << ps[1].calories << "\n";
  ps[2].brand = "M&Ms";
  ps[2].weight = 2.4;
  ps[2].calories = 28;
  cout << "\nBrand: " << ps[2].brand;
  cout << "\nWeight: " << ps[2].weight;
  cout << "\nCalories: " << ps[2].calories << "\n";
  delete [] ps;
  cin.get();
  return 0;
}
char brand[];
is not legal.

When you declare an array, you need to tell it how many elements it has. Also, you cannot assign a value to an array of characters simply by saying
brand = "string";

You have two options. You can change it to a pointer:
char *brand;

or (I recommend this) you can use a string instead:
string brand;

If you use the string, you'll need to add
#include <string>
to your list of headers.
Ah ha!!!! Thank you all very much! Mwahahahaha! (Continues to laugh maniacally as he compiles his program).

Sorry, I also do some creative writing. Anyway, the program compiled successfully, and thanks again.
Topic archived. No new replies allowed.