C 如何初始化嵌套结构

C 如何初始化嵌套结构,c,C,我将此代码保存在一个文件中: struct ReadyQueue { struct PCB *pcb; struct ReadyQueue *next; }; extern struct CPU new_cpu; typedef struct ReadyQueue *node; node* head; node createNode() { node temp; temp = (node)malloc(sizeof(struct ReadyQueue));

我将此代码保存在一个文件中:

struct ReadyQueue {
    struct PCB *pcb;
    struct ReadyQueue *next;
};

extern struct CPU new_cpu;

typedef struct ReadyQueue *node;

node* head;

node createNode() {
    node temp;
    temp = (node)malloc(sizeof(struct ReadyQueue));

    if (temp == NULL) {
        printf("Error creating node");
        exit(0);
    }

    temp->next = NULL;
    free(temp);

    return temp;
}

node* head;

void addToReady(FILE *p) {

    node temp;
    temp = createNode();

    temp->pcb->PC = p;  // error here

    if (temp == NULL) {
        printf("Error");
    }

}
这将创建节点的链接列表。在我的addToReady函数中,设置PCB值时遇到问题,该值在另一个文件中声明了另一个结构:

struct PCB {
  FILE *PC;
};

我在
temp->pcb->PC=p
中出现错误,它给了我一个
分段错误11
错误。

对结构进行malloc操作不会在其中执行malloc。你也需要为会员提供mallocs
free(temp);返回温度?这意味着您将返回一个悬空指针。请不要将指针隐藏在typedef后面。这使得代码更难理解。为什么不干脆
struct-PCB在您的结构中?为什么在那里有一个指针?取消引用后检查temp是否为NULL并没有多大用处,因为删除结构不会在其中执行malloc。你也需要为会员提供mallocs
free(temp);返回温度?这意味着您将返回一个悬空指针。请不要将指针隐藏在typedef后面。这使得代码更难理解。为什么不干脆
struct-PCB在您的结构中?为什么在那里有一个指针?在取消引用后检查temp是否为NULL也没什么用处