使用new来动态分配结构体数组,初级C++问题

心态与习惯
博客专家认证
2014-03-02 05:48:11
看C++premier plus第四章课后题第九题有这样一道题:
结构CandyBar包含3个成员。第一个成员存储了糖块的品牌,第二个成员存储糖块的重量(小数),第三个成员存储了糖块的卡路里含量(整数)。
创建一个包含3个元素的CandyBar数组,使用new来动态分配数组,并初始化,然后显示。
刚自学C++,网上没搜到答案,所以来论坛发帖。
...全文
10499 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_57767777 2021-08-02
  • 打赏
  • 举报
回复

为什么没用delete

dzkjdxzxl 2016-07-13
  • 打赏
  • 举报
回复
dzkjdxzxl 2016-07-13
  • 打赏
  • 举报
回复
qzk1993 2016-06-29
  • 打赏
  • 举报
回复 1
引用 6 楼 robert_chen1988 的回复:
[quote=引用 3 楼 fwewfgs 的回复:] 结构体定义
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;
	}
明白了,这样可以,但我想用bar[i]->.a不行[/quote] 想用指针的话可以这样 (bar+i)->a;
f_liu 2014-03-11
  • 打赏
  • 举报
回复
引用 8 楼 robert_chen1988 的回复:
[quote=引用 7 楼 fwewfgs 的回复:] 指针才用 "->",不是指针用"."
CandyBar *bar = new CandyBar[5]; bar 不是指针吗?[/quote]但bar[i]不是指针...
心态与习惯 2014-03-11
  • 打赏
  • 举报
回复
引用 7 楼 fwewfgs 的回复:
指针才用 "->",不是指针用"."
CandyBar *bar = new CandyBar[5]; bar 不是指针吗?
f_liu 2014-03-03
  • 打赏
  • 举报
回复
指针才用 "->",不是指针用"."
心态与习惯 2014-03-02
  • 打赏
  • 举报
回复
引用 3 楼 fwewfgs 的回复:
结构体定义
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;
	}
明白了,这样可以,但我想用bar[i]->.a不行
深度抽象 2014-03-02
  • 打赏
  • 举报
回复
看一下这个就懂了
#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];
}
derekrose 2014-03-02
  • 打赏
  • 举报
回复
引用 2 楼 robert_chen1988 的回复:
[quote=引用 1 楼 derekrose 的回复:]

struct CandyBar{
string brand;
double weight;
int calorie;
};

struct CandyBar* pCandyBar = new struct CandyBar[3];
...
但是怎么赋值呢[/quote] []
f_liu 2014-03-02
  • 打赏
  • 举报
回复
结构体定义
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;
	}
心态与习惯 2014-03-02
  • 打赏
  • 举报
回复
引用 1 楼 derekrose 的回复:

struct CandyBar{
string brand;
double weight;
int calorie;
};

struct CandyBar* pCandyBar = new struct CandyBar[3];
...
但是怎么赋值呢
derekrose 2014-03-02
  • 打赏
  • 举报
回复

struct CandyBar{
string brand;
double weight;
int calorie;
};

struct CandyBar* pCandyBar = new struct CandyBar[3];
...

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧