c - 如何删除 ':' token 之前的错误 : expected ',' , ';' 、 '}' 、 '__attribute__' 或 '=' ?

标签 c data-structures

program picture

#include <stdio.h>
#include <stdlib.h>

struct tree {
    int data;
    struct tree *left, *right;
};

struct queue {
    struct tree **nodeQ = (struct tree**)malloc(sizeof(struct tree*));
    int front;
    int rear;
};

最佳答案

在 C 中,您无法像尝试使用 nodeQ 成员那样内联初始化结构成员。

创建结构体时需要初始化成员。

所以你需要做类似的事情

struct queue q = { malloc(sizeof(struct tree *)), 0, 0 };

struct queue *q = malloc(sizeof(struct queue));
q->nodeQ = malloc(sizeof(struct tree *));
q->front = 0;
q->rear = 0;

请注意,我 do not cast the result of malloc .

关于c - 如何删除 ':' token 之前的错误 : expected ',' , ';' 、 '}' 、 '__attribute__' 或 '=' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35964013/

相关文章:

c - _f_data_rom 链接描述文件符号

C - 不兼容的指针到整数转换?

c - isDigit() 一直返回 0,即使它是数字,C 语言

data-structures - 此排序数组数据结构的集合有名称吗?

c# - 在 32 位和 64 位运行时编码(marshal)结构时的不同行为

C: 代替从分配它的变量打印字符串?

c - c 如何计算包含前缀增量的表达式?

algorithm - 排序需要多少操作?

java - 选择最佳数据结构

algorithm - 图实现、函数和参数。什么更有意义?