问题:
解答:
#include <iostream>
using namespace std;
typedef struct _CandyBar
{
string brand;
float weight;
int calorie;
}CandyBar;
int main()
{
CandyBar* snack = new CandyBar[3];
snack[0] = {"德芙",2.1,20};
snack[1] = { "箭牌",2.2,16 };
snack[2] = { "阿尔卑斯",2.3,18 };
for (int i = 0; i < 3; i++)
{
cout << "品牌:" << snack[i].brand << endl;
cout << "重量:" << snack[i].weight << endl;
cout << "卡路里:" << snack[i].calorie << endl << endl;
}
delete[]snack;
return 0;
}
运行结果:
考查点:
- new分配数组内存
注意:
- 有new就一定要有delete,数组要加[]
2024年8月24日20:52:45