使用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创建的动态结构数组赋值方法还是有问题!应该还有更简单的
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了