65,170
社区成员




为什么没用delete
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
struct CandyBar{
CandyBar():brand(""),weight(0.0),calorie(0){
printf("brand:%s,weight:%lf,calorie:%d\n",brand.c_str(),weight,calorie);
}
string brand;
double weight;
int calorie;
};
int main()
{
struct CandyBar* pCandyBar = new struct CandyBar[3];
}
struct CandyBar
{
string a; //糖块的品牌
double b; //储糖块的重量(小数)
int c; //卡路里含量(整数)
};
动态分配,你可以修改数组大小:
CandyBar *bar = new CandyBar[5];
赋值,当然你也可以自己输入:
for(int i = 0;i < 3;i ++)
{
bar[i].a = "fwe";
bar[i].b = 1.1;
bar[i].c = 3;
}
struct CandyBar{
string brand;
double weight;
int calorie;
};
struct CandyBar* pCandyBar = new struct CandyBar[3];
...