您的位置:首页 > 其它

使用new创建动态结构

2017-03-25 10:11 796 查看
#include <iostream>
struct inflatable //struck defination
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable* ps= new inflatable;//为结构体分配内存空间
cout<<"Enter name of inflatable item: ";
cin.get(ps->name,20);//method 1 for member access
cout<<"Enter volume in cubic feet: ";
cin>>(*ps).volume;//method 2 for member acess
cout<<"Enter price: $";
cin>>ps->price;
cout<<"Name: "<<(*ps).name<<endl;//method 2
cout<<"Volume: "<<ps->volume<<" cubic feet\n";//method 1
cout<<"Price: $"<<ps->price<<endl;//method 2
//如果结构标识符是结构名,则使用句点运算符;如果标识符是指向结构的指针,则使用箭头运算符
delete ps;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: