#include <iostream>
using namespace std;
// 定义一个结构体类型
struct Person {
string name;
int age;
// 构造函数
Person(const string& n, int a) : name(n), age(a) {}
};
int main() {
// 创建一个 Person 类型的指针数组,长度为3
Person* people[3];
// 动态分配内存并初始化每个指针
people[0] = new Person("Alice", 30);
people[1] = new Person("Bob", 25);
people[2] = new Person("Charlie", 20);
// 输出每个人的信息
for (int i = 0; i < 3; ++i) {
cout << "Name: " << people[i]->name << ", Age: " << people[i]->age << endl;
}
// 新增的代码
const Person ** pp = people;
// 释放分配的内存
for (int i = 0; i < 3; ++i) {
delete people[i];
}
return 0;
}
using namespace std;
// 定义一个结构体类型
struct Person {
string name;
int age;
// 构造函数
Person(const string& n, int a) : name(n), age(a) {}
};
int main() {
// 创建一个 Person 类型的指针数组,长度为3
Person* people[3];
// 动态分配内存并初始化每个指针
people[0] = new Person("Alice", 30);
people[1] = new Person("Bob", 25);
people[2] = new Person("Charlie", 20);
// 输出每个人的信息
for (int i = 0; i < 3; ++i) {
cout << "Name: " << people[i]->name << ", Age: " << people[i]->age << endl;
}
// 新增的代码
const Person ** pp = people;
// 释放分配的内存
for (int i = 0; i < 3; ++i) {
delete people[i];
}
return 0;
}