使用new和delete在堆中申请内存

具体看实例:

 1 #include <iostream>
 2 using namespace std;
 3 //定义一个第一人称游戏的人物角色的结构体
 4 struct NPC {
 5     char Name[20];
 6     int age;
 7     bool sex;//男=0,女=1
 8 };
 9 int main() {
10     int *p1 = new int(5);
11     int *p2 = new int[3]{100,200,300};
12     NPC *p3 = new NPC{"mk",17,0};
13     cout << p3->Name <<","<< p3->age << "," << p3->sex << endl;
14     cout << *p1 << endl;
15     cout << *p2 << "," << *(p2+1) << "," << *(p2+2) << endl;
16     system("pause");
17     delete p1;
18     delete p2;
19     delete p3;
20     return 0;
21 }

猜你喜欢

转载自www.cnblogs.com/mktest123/p/12114289.html
今日推荐