C++中用new创建动态结构数组并同时初始化输出问题?
#include<iostream>#include<string>usingnamespacestd;structCandyBar{charname[20];doubl...
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{char name[20];
double wight;
int ka;
};
int main()
{int i;
CandyBar * ps=new CandyBar[2];
*ps=
{
{"Mocha Munch",2.3,350},
{"xiao sheng",5.1,888},
{"xiao qiang",5.0,666}
};
for(i=0;i<3;i++)
{cout<<(ps+i)->name<<endl<<(ps+i)->wight<<endl<<(ps+i)->ka<<endl;};
delete [] ps;
return 0;
}
如何给CandyBar[2]初始化并全部输出动态数组啊?小弟是新手希望各位前辈能帮我把程序改好,我不是没动脑筋,但是我做到上面就真的不会了,拜托各位了,谢谢啊~! 展开
#include<string>
using namespace std;
struct CandyBar
{char name[20];
double wight;
int ka;
};
int main()
{int i;
CandyBar * ps=new CandyBar[2];
*ps=
{
{"Mocha Munch",2.3,350},
{"xiao sheng",5.1,888},
{"xiao qiang",5.0,666}
};
for(i=0;i<3;i++)
{cout<<(ps+i)->name<<endl<<(ps+i)->wight<<endl<<(ps+i)->ka<<endl;};
delete [] ps;
return 0;
}
如何给CandyBar[2]初始化并全部输出动态数组啊?小弟是新手希望各位前辈能帮我把程序改好,我不是没动脑筋,但是我做到上面就真的不会了,拜托各位了,谢谢啊~! 展开
1个回答
展开全部
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
char name[20];
double wight;
int ka;
};
int main()
{
int i;
CandyBar *ps =(CandyBar*) new CandyBar[3];
// *ps = {"MochaMunch",2.3,350};
// ps[1] = {"xiaosheng",5.1,888};
// ps[2] = {"xiaoqiang",5.0,666};
strcpy((ps+0)->name, "CandyBar");
(ps+0)->wight = 100;
(ps+0)->ka = 100;
for(i=0;i<3;i++)
{
cout<<(ps+i)->name<<endl<<(ps+i)->wight<<endl<<(ps+i)->ka<<endl;
}
delete []ps;
return 0;
}
照个上面的
strcpy((ps+0)->name, "CandyBar");
(ps+0)->wight = 100;
(ps+0)->ka = 100;
写剩余的两个了
#include<string>
using namespace std;
struct CandyBar
{
char name[20];
double wight;
int ka;
};
int main()
{
int i;
CandyBar *ps =(CandyBar*) new CandyBar[3];
// *ps = {"MochaMunch",2.3,350};
// ps[1] = {"xiaosheng",5.1,888};
// ps[2] = {"xiaoqiang",5.0,666};
strcpy((ps+0)->name, "CandyBar");
(ps+0)->wight = 100;
(ps+0)->ka = 100;
for(i=0;i<3;i++)
{
cout<<(ps+i)->name<<endl<<(ps+i)->wight<<endl<<(ps+i)->ka<<endl;
}
delete []ps;
return 0;
}
照个上面的
strcpy((ps+0)->name, "CandyBar");
(ps+0)->wight = 100;
(ps+0)->ka = 100;
写剩余的两个了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询