|
Someone asked me a question, the code is as follows
#include <iostream>
using namespace std;
struct pizza
{
char company [3];
double diameter;
double weight;
};
int main ()
{
pizza * ps = new pizza [3];
ps [0] .company = "qq";
ps [0] .diameter = 1.2;
ps [0] .weight = 1.2;
ps [1] .company = "w";
ps [1] .diameter = 2.2;
ps [1] .weight = 2.2;
ps [2] .company = "e";
ps [2] .diameter = 3.2;
ps [2] .weight = 3.2;
cout << ps [0] .company << endl;
cout << ps [0] .diameter << endl;
cout << ps [0] .weight << endl;
cout << ps [1] .company << endl;
cout << ps [1] .diameter << endl;
cout << ps [1] .weight << endl;
cout << ps [2] .company << endl;
cout << ps [2] .diameter << endl;
cout << ps [2] .weight << endl;
delete [] ps;
cin.get ();
cin.get ();
return 0;
}
The above error should be a problem with character array constants (I use java more, c ++ is still a bit of the impression I used to see), ps [0] .company = "qq"; This definitely does not work for string constants. I changed char company [3]; in the structure to char * company;
My question is, how to not change the definition of the structure, that is, also use char company [3] ;, modify the assignment statement, and assign by pointer? (Not sure how to get a pointer to the company element)
What a mess, prawns understand? |
|