这可能是一个简单的解决办法,我不是熟悉C只是想端口我的Java数据结构分配到C.点作为函数的参数ç
这是我收到的错误:
测试.C:4:错误:预期之前 ')' '*' 令牌
test.c的:11:错误:预期 ')' 之前, '*' 令牌
#include <stdio.h>
#include <stdlib.h>
void to_screen(NODE *cur){
while(cur->next != NULL){
printf("%d\n", cur->data);
cur = cur->next;
}
}
void add_first(NODE *head, int data){
NODE *cur;
int i;
for(i=0; i<10; i++){
cur = malloc(sizeof(NODE));
cur->data = data;
cur->next = (*head).next;
head->next = cur;
}
}
typedef struct node{
int data;
struct element *next;
}NODE;
int main(){
int i;
NODE *head;
for(i=0; i<10; i++){
add_first(head, i);
}
to_screen(head);
}
除了将typedef移动到顶端,不应该读为:'typedef struct node {int data; struct node * next; }节点;'? – 2010-08-16 02:53:49
是的,这解决了我的问题的最后一部分,谢谢 – Kyle 2010-08-16 02:56:35
此代码还有另一个问题。 main函数中的NODE * head可能指向任何地方,它也是'add_first()'的参数。由于'add_first()'通过'head-> next'读取头部,所以会导致分段错误。 – czchen 2010-08-16 03:08:12