c - 如何访问结构数组中的变量

标签 c arrays struct structure

在下面的 for 循环中,我尝试访问存储在另一个结构中的结构数组,但我继续收到一条错误消息。 “结构列表没有成员头。”

list* createList(int size)
{
    list* graph = malloc(sizeof(list));
    graph->size = size;
    graph->array = malloc(size * sizeof(vertex));
    int i;
    for(i=0; i < size;i++){
        graph->array[i].head = NULL;
        return graph;
    }
}

我尝试使用的结构如下。

struct vertex
{
    struct vertex *head;
};
typedef struct vertex vertex;

struct list
{
    int size;
    struct list* array;
};
typedef struct list list;

最佳答案

你的 list 成员是 list 类型,而它应该是 vertex 类型。

关于c - 如何访问结构数组中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21739615/

相关文章:

python - 从其他 Cython 声明文件导入外部声明

arrays - Swift 按特定顺序加入 2 个数组

C - 在没有头文件的情况下在多个文件中构造

C - 这行代码是什么意思?(关于结构)

c - 暂停一个linux内核函数

c - C 中的不兼容转换错误

c - 在这种情况下 && 和 & 有什么不同?

arrays - 从数组中删除自定义类的元素

arrays - swift 数组 : Cannot invoke 'append' with an argument list of type '(Int)'

c - Strcpy 改变 struct int 数组