别再只盯着.ts文件了!手把手带你读懂M3U8里的KEY和IV,搞懂主流视频平台加密套路
2026/4/23 12:11:04
指向结构体变量的指针,本质还是指针。
struct Student { int id; string name; double score; }; Student stu = {1001, "张三", 90.5}; // 结构体指针 Student *p = &stu;.->// 正确 cout << p->id << endl; cout << p->name << endl; cout << p->score << endl;p->score = 95.0;普通结构体数组长度固定,动态结构体数组可以在运行时指定人数,用 new 创建。
int n; cout << "请输入学生人数:"; cin >> n; // 动态开辟 n 个 Student Student *arr = new Student[n];两种写法完全等价:
arr[i].name; // 或 (arr + i)->name;cpp
运行
delete[] arr; arr = nullptr;功能:
#include <iostream> #include <string> using namespace std; struct Student { int id; string name; double score; }; // 打印单个学生 void showStudent(Student *p) { cout << "学号:" << p->id << "\t姓名:" << p->name << "\t成绩:" << p->score << endl; } int main() { int n; cout << "请输入学生人数:"; cin >> n; // 动态开辟结构体数组 Student *arr = new Student[n]; // 录入信息 for (int i = 0; i < n; i++) { cout << "请输入第" << i + 1 << "个学生信息(id 姓名 成绩):" << endl; cin >> arr[i].id >> arr[i].name >> arr[i].score; } // 遍历输出 cout << "\n===== 学生列表 =====" << endl; for (int i = 0; i < n; i++) { showStudent(&arr[i]); } // 找最高分 int maxIndex = 0; for (int i = 1; i < n; i++) { if (arr[i].score > arr[maxIndex].score) { maxIndex = i; } } cout << "\n最高分学生:" << endl; showStudent(&arr[maxIndex]); // 释放动态内存 delete[] arr; arr = nullptr; system("pause"); return 0; }// 用指针,避免拷贝,效率更高 void setScore(Student *p, double s) { p->score = s; }调用:
Student stu = {1001, "张三", 80}; setScore(&stu, 99);比值传递更快,尤其结构体很大时。
.而不是->delete释放,应该用delete[]->,变量用.Student *arr = new Student[n]delete[]并置空