c - C - 嵌套链表
问题描述
我正在尝试创建一个学生链接列表,每个学生都有一个成绩链接列表,但是我无法访问学生链接列表中的成绩链接列表。
typedef struct student_data_struct{
char student[MAX];
struct grades_list_struct *gradeP;
} student_Data;
typedef struct student_list_struct{
student_Data studentData;
struct student_list_struct *next;
} StudentNode;
typedef struct grades_list_struct{
int grade;
struct grades_list_struct *next;
} GradeNode;
GradeNode *insertGrade(int grade, GradeNode *head){
GradeNode *newNode=NULL;
newNode=(GradeNode*)calloc(1, sizeof(GradeNode));
if(head!=NULL){
newNode->grade=grade;
newNode->next=head;
return newNode;
} else {
newNode->grade=grade;
newNode->next=NULL;
return newNode;
}
}
StudentNode *insertStudent(char studentName[MAX], int studentGrade, StudentNode *head){
StudentNode *newNode=NULL;
newNode=(StudentNode*)calloc(1, sizeof(StudentNode));
newNode->studentData->gradeP=(GradeNode*)calloc(1, sizeof(GradeNode));
if (head==NULL){
strcpy(newNode->studentData.student, studentName);
newNode->next=NULL;
newNode->studentData->gradeP=insertGrade(studentGrade, newNode->studentData->gradeP);
return newNode;
} else {
strcpy(newNode->student, studentName);
newNode->gradeP->grade=studentGrade;
newNode->studentData->gradeP=insertGrade(studentGrade, newNode->studentData->gradeP);
return newNode;
}
}
当我尝试为等级指针分配内存时,
newNode->studentData->gradeP=(GradeNode*)calloc(1, sizeof(GradeNode));
我得到错误:
error: invalid type argument of '->' (have 'student_Data' {aka 'struct student_data_struct'})
同样,当我尝试为学生插入成绩时,
newNode->studentData->gradeP=insertGrade(studentGrade, newNode->studentData->gradeP);
我得到错误:
error: invalid type argument of '->' (have 'student_Data' {aka 'struct student_data_struct'})
任何帮助将不胜感激。
解决方案
studentData
是结构类型,不是指向结构的指针。因此,必须使用 struct() 的成员访问运算符.
而不是运算符来访问指向 struct( ->
) 的指针的成员。
推荐阅读
- android - 带有 Retrofit 2 Cache-Control 的离线模式
- java - 错误:“items”不支持 c:forEach jsp 中的运行时表达式
- r - 在购物篮分析中,我有什么办法只能在关联规则的 lhs 中获得 1 项?
- php - 如何一次抛出错误消息?
- java - 递归结构上的 DTO 投影
- angularjs - Angularjs Ng-init 在通过 ng-repeat 时显示相同的检索到的表单值
- db2 - DB2 锁定场景
- elasticsearch - 如何在 Elasticsearch 上索引混合语言内容?
- python-3.x - rasa nlu 多模型 python
- android - 无法使用 Webview android 获取 HTTP 401 错误的回调