I have a problem to assign a string literal to an array of chars. This is my code:
#include <iostream>
using namespace std;
struct CandyBar{
char brand[20];
double weight;
int calories;
};
int main()
{
char a[20] = "Mocha Munch";
cout << a;
CandyBar snack;
snack.brand = "Mocha Munch";
snack.weight = 2.3;
snack.calories = 350;
cout << "Brand of snack: " << snack.brand << endl;
cout << "Weight of snack: " << snack.weight << endl;
cout << "Calories of snack: " << snack.calories << endl;
return 0;
}
My question is why with a[20] I can assign it to the array, but with brand I cannot.
a[20]I can assign it to the array" You can't.