【发布时间】:2017-10-18 16:58:07
【问题描述】:
我在尝试创建链接列表时不断收到错误消息。在我尝试malloc() 新节点的两行中,错误是“'struct' 之前的预期表达式”。我查看了类似的问题并尝试修复我的代码,但我无法让它工作。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
struct List {
int x;
struct List *next;
};
int main() {
struct List* head = (struct List*)malloc(sizof(struct List));
if (head == NULL) {
return 1;
}
head->x = 1;
head->next = (struct List*)malloc(sizof(struct List));
head->next->x = 2;
head->next->next = NULL;
struct List* current = head;
while(current != NULL) {
printf("%d", current->x);
current = current->next;
}
return 0;
}
【问题讨论】:
-
不应该是
sizeof而不是sizof吗? -
sizof()-->>sizeof() -
另外,你不需要转换
malloc()的返回值。
标签: c struct linked-list