這可能是一個簡單的解決辦法,我不是熟悉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