使用new创建动态结构

来源:互联网 发布:linux 查看文件字符集 编辑:程序博客网 时间:2024/06/03 02:40
#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;}

0 0
原创粉丝点击