c - 错误:取消引用指向不完整类型“struct student”root->next->student_number = 17 的指针;
问题描述
//------libraries
#include <stdio.h> //library for basic input/output
#include <stdlib.h> //library for memory allocations
//------structures
typedef struct{
unsigned short int student_number;
struct student *next;
}student;
int main( void){
student *root;
root = (student *)malloc(sizeof(student));
root->student_number = 5; //works
printf("root student_number = %d\n", root->student_number);
root->next = (student *)malloc(sizeof(student));
root->next->student_number = 17; //here is the problem
printf("root->next = %d\n", root->next->student_number);
}
我创建了一个结构student。定义了一个指向链表第一个元素的根。有可能到达第一个链表元素(print root->student_number)。直到这里一切都很好:为链表的第二个元素分配了内存(root->next),但我不能为变量 student_number 赋值,分别打印。如何到达链表的第二个和下一个元素?会很高兴得到帮助:)
解决方案
你typedef
编辑了一个匿名结构。 struct student
不存在。尝试这个:
typedef struct student{
unsigned short int student_number;
struct student *next;
}student;
推荐阅读
- java - 如何强制创建控制皮肤以便我可以使用 getParent?
- amazon-web-services - Systemd 服务文件命令不起作用,但可与直接命令一起使用
- python - 如何确定一个人是否有硝基不和谐 py
- python - 如何创建一个直方图,显示相对于另一个变量的指定范围的二项式值的计数
- puppeteer - 在同一个浏览器上使用多个 Chrome 配置文件?
- elasticsearch - Metricbeat 检索总 CPU 总体利用率(而不是“使用”或“负载”)
- javascript - 我的应用突然说找不到我的 app.tsx 模块
- react-native - 使用 react-native-circular-progress 中的半圆形,从另一端没有显示圆形
- ios - SceneKit SCNAnimation:如何检查当前是否正在播放某个动画?
- sql - 如何使用 SQL 查询从 JSON 数据中查找特定列的计数