使用new来为动态分配结构数组并赋值
// practice4-9.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
int _tmain(int argc, _TCHAR* argv[])
{
int i;
CandyBar * ps =new CandyBar[3];
//为动态结构数组赋值
ps[0].brand="pizza hut";
ps[1].brand="pizza down";
cout<<"Enter ps[2] brand:";
getline(cin,ps[2].brand);
cout<<"Enter weight:";
cin>>ps[2].weight;
cout<<"Enter calorie:";
cin>>ps[2].calorie;
for (i=0;i<3;++i)
{
cout<<ps[i].brand<<endl;
}
delete [] ps;
system("pause");
return 0;
}
为new创建的动态结构数组赋值方法还是有问题!应该还有更简单的声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: vue实战总结
- 下一篇: 怎样获取字符串数组的长度
