知识点小结:
当需要批量赋值结构体的时候,可以使用结构体数组。对于结构体指针,可以用来指向结构体数组的首地址,但是不能按照结构体数组的赋值方式进行赋值,感觉是无法预知后面批量赋值的大小。具体情况见以下代码:
#include <iostream>
using namespace std;
struct student
{
string name;
int score;
};
int main()
{
//结构体指针不能像结构体数组一样直接赋值
//不能写成 student *stlist = {{"a", 90}, {"b", 95}};
student stlist[] = {{"a", 90}, {"b", 95}};
cout << stlist[0].name << " " << stlist[0].score << endl;
cout << stlist[1].name << " " << stlist[1].score << endl;
//new 的时候一定要指明空间大小
student *stlist2 = new student[2]{{"a", 90}, {"b", 95}};
return 0;
}