typedef struct mensagem
{
int sender ;
int receiver ;
char *text ;
} *Item ;
typedef struct node
{
Item item ;
struct node *next ;
} *link ;
link init(char* text)
{
link x = (link) malloc(sizeof(struct node));
(x->item->text) = (char*) malloc(sizeof(char)*(strlen(text)+1));
strcpy(x->item->text, text);
x->next = NULL;
return x;
}
我打算使用 item 内的数据,但我在线上遇到了段错误:
(x->item->text) = (char*) malloc(sizeof(char)*(strlen(text)+1));
我对 C 和指针相当陌生,但我在这里找不到问题。
最佳答案
您尚未为 x->item 指向的结构分配内存。添加
x->item = malloc(sizeof (struct mensamam));
在其他 malloc 之前。
关于C : Acessing a structure within a structure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23574844/