gpt4 book ai didi

c - 简单链表段错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:46 25 4
gpt4 key购买 nike

我是 C 编程语言的新手。

我正在学习有关链表的 C 语言,试图打印“hello world”,但我遇到了段错误。

我正在使用文本编辑器 (vi) 和 gcc。我如何跟踪错误,哪一部分导致段错误,以及如何解决这个问题?

我应该把 printf 放在每一行吗?我将不胜感激任何帮助/一些建议

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


typedef struct {
int variable;
} abc_create_t;

typedef struct {
int variable;
} pdn_con_t;

typedef struct pdn_con_list_t_slot {
pdn_con_t conn;
struct pdn_con_list_t_slot *next, *prev;
} pdn_con_list_t_slot;

typedef struct {
pdn_con_list_t_slot *head, *tail;
} pdn_con_list_t;

typedef struct {
int variable;
pdn_con_list_t connections;
} gprs_t;

void send_Method(gprs_t *ue, abc_create_t *msg)
{
//assert(ue->connections.head);
printf("IN THIS BLOCK");
}

int main()
{

gprs_t *ue = NULL;
ue->variable=1;
abc_create_t *msg = NULL;
msg->variable=1;
send_Method(ue, msg);
return 0;
}

最佳答案

兄弟,你还没有分配内存并尝试在其中存储值。你需要先使用ma​​lloc()分配内存并使结构指针指向到它,然后只有你可以继续工作。

声明结构不会为其元素分配内存。您必须这样做。

int main()
{

gprs_t *ue = NULL;
ue= (gpre_t *) malloc(sizeof(gprs_t));
ue->variable=1;
//Rest of the code

}

关于c - 简单链表段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6199632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com