c - C LinkedList 指针不适用于动态分配的数组
问题描述
我正在尝试使用指针分配 LinkedList 变量,但出现错误。这是我的代码。
typedef struct {
struct Vertex *next;
} Vertex;
typedef struct {
Vertex *vertices;
} Environment;
// here I dynamically allocate an array of vertexs, 28 to be specific
env->vertices = (Vertex *)malloc(7*sizeof(Vertex));
//this code is just to understand what I am trying to achieve
env->vertices[0].next = env->vertices[1];
我收到一条错误消息,提示我正在尝试将 Vertex 分配给 Vertex*。有任何想法吗?
解决方案
传递地址正是您需要做的。您只是忘记了单词的一个实例Vertex
。你的第一个声明应该是这样的:
typedef struct Vertex {
struct Vertex *next;
} Vertex;
一旦你解决了这个问题,你就可以不用env->vertices[0].next = &env->vertices[1];
不兼容的指针类型警告。
推荐阅读
- ag-grid - 启用 groupMultiAutoColumn 时动态更新自动生成的组列
- firebase - 为什么 node.js 中的 Firebase 管理员身份验证不使用 ADC?
- arrays - 使用嵌套数组定义 JSON 模式
- crystal-reports - 向报表添加第二个表字段会导致报表变为空白 - Crystal Reports 14.1.2.1121 使用 SQL Server 数据库源
- javascript - 将鼠标悬停事件插入使用 SVGRenderer、highcharts 创建的自定义标签
- javascript - XMLHttp 请求更新数据库中的数据
- node.js - 嵌套对象上的 Mongo 索引
- c++ - 如何从向量中删除重复的所有实例
- sql - 从提款金额中减去每日金额
- r - 过滤包含条件和异常的数据集