知识点小结:
当需要批量赋值结构体的时候,可以使用结构体数组。对于结构体指针,可以用来指向结构体数组的首地址,但是不能按照结构体数组的赋值方式进行赋值,感觉是无法预知后面批量赋值的大小。具体情况见以下代码:
#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;
}
本文介绍了在C++中如何使用结构体数组进行批量赋值,并探讨了结构体指针的应用及其限制条件。通过示例代码展示了正确的结构体数组初始化方式与使用结构体指针时应注意的问题。
4255

被折叠的 条评论
为什么被折叠?



