tags:

views:

74

answers:

3

this is probably a simple solution I am not that familiar with C just trying to port my java data structure assignments to C.

this is the error i am getting:

test.c:4: error: expected ‘)’ before ‘*’ token

test.c:11: error: expected ‘)’ before ‘*’ token

#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);
}
+1  A: 

You need to define NODE before it is used. Move the definition to the top.

tc.
+4  A: 

You need to move the definition of your struct above the to_screen function. The compiler is saying that it doesn't know what NODE is.

obelix
Thank you, that was it.
Kyle
A: 

You need to move this block to the top as 2 other answers recommend.

typedef struct node{
    int data;
    struct element *next;
}NODE;

You may ask for the reason. The reason is that C language specification is not like Java. So it kinds of compiling from the top to the bottom. So, if it see something undefined, it will look for definition above that point, and if it sees the definition it gets it. It doesn't look below the line of code

vodkhang
@vodkhang. It's not about the compiler being smart or dumb. It's about being single-pass or multi-pass.
Juan Pablo Califano
It's about the *language specification* not the compiler.
strager
Ops, ok. I don't know about that. I always blame the compiler
vodkhang