【发布时间】:2021-05-25 08:43:45
【问题描述】:
这是我的代码:
struct Node{
int data;
char nim[12];
struct Node *next, *prev;
};
struct Node *head, *tail;
void init(){
head = NULL;
tail = NULL;
}
int isEmpty(struct Node *h){
if(h==NULL)
return 1;
else
return 0;
}
void addData(char *nimI){
struct Node *baru;
baru = malloc(sizeof *baru);
baru->nim = malloc(12 * sizeof(char));
strcpy(baru->nim, nimI);
baru->next = NULL;
baru->prev = NULL;
if(isEmpty(head)==1){
head=baru;
tail=baru;
}else{
tail->next=baru;
baru->prev=tail;
tail = baru;
}
printList(head);
}
int main()
{
char nimI[12];
printf("NIM : ");
scanf("%[^\n]#", &nimI);
fflush(stdin);
addData(nimI);
}
我想在我的双向链表中输入char,但是出错了。
错误:
分配给数组类型的表达式(baru 中的错误->nim = malloc(12 * sizeof(char));)
【问题讨论】:
-
char nim[12];已经声明了一个 12 个字符的数组,它将作为分配结构的一部分进行分配。你为什么要再次为它分配内存?如果您真的想将其单独分配给结构,请将其更改为char *nim;。 -
它的工作,但输出是变量的地址而不是值@kaylum
标签: c linked-list char malloc doubly-linked-list