//1、 将new用于结构由两部分组成:创建结构和访问其成员。要创建结构需要同时使用结构类型和new,
//例如:要创建一个未命名的inflatable类型。并将其地址赋值给一个指针。
// inflatable *ps = new inflatable;
struct things
{
int good;
int bad;
};
things grubnose ={3,453};
things *pt =&grubnose;
//2、如果结构标识符是结构名,则使用句点运算符。grubnose.good
//3、如果标识符是指向结构的指针,则使用箭头运算符。pt ->good
//4【举例】
#include<iostream>
struct inflatable
{
char name [20];
float volume;
};
int main()
{
using namespace std;
inflatable *ps=new inflatable;
cin.get(ps->name,20);
cin>>(*ps).volume;
cout<<(*ps).name<<endl;
cout << ps->volume<<endl;
delete ps;
}
使用new来创建动态结构
最新推荐文章于 2024-05-29 22:35:05 发布