现在的位置: 首页 > 综合 > 正文

C++使用new创建动态结构

2019年01月05日 ⁄ 综合 ⁄ 共 496字 ⁄ 字号 评论关闭
<pre name="code" class="cpp">#include <iostream>

using namespace std;
struct inflatable{
	char name[20];
	float volume;
	double price;	
};
/**
 * 调用结构成用时用成员运算符 ps->price
 *也可以用*ps 调用 	
 */
int main(){
	inflatable *ps = new inflatable;
	cout <<"请输入名称:"<<endl;
	cin.get(ps->name,20);
	cout <<"请输入volume"<<endl;
	cin>>(*ps).volume;
	cout <<"请输入price:"<<endl;
	cin>>(*ps).price;
	cout <<"name"<<(*ps).name<<endl;
	cout <<"volume"<<ps->volume<<endl;
	cout<<"price"<<ps->price<<endl;
	delete ps; 
	return 0;
}

动态创建结构说白了就是内存分配不是在编译的时候分配的而是运行的时候分配内存的,也就是new的时候分配的请看下面的例子就明白了。

抱歉!评论已关闭.