Struct declaration in C -
i have simple program in pure c, reading records file , putting linked list. not allowed use global variables. program looks this:
here includes #defines function headers list manipulation: void add_item(record * p, ll * ll); void print_all(ll * ll); void load(ll * ll); ... int main(int argc, char const *argv[]) { // sample struct defining 1 record typedef struct record { char sign[10]; long int isbn; char name[100]; char authors[100]; long int date; int id; struct record * next; } record; // information linked list (ll) typedef struct ll { record * head; }ll; // create new linked list ll * ll = (ll *) malloc(sizeof(ll)); // init ll->head = null; // other work ll, record ... } // functions self // add item p ll void add_item(record * p, ll * ll) { if (ll->head == null) { ll->head = p; ll->head->next = null; } else { record * cur = ll->head; while(cur->next != null) cur = cur->next; cur->next = p; } } void print_all(ll * ll) { if (!ll->head) { printf("%s\n", "ll->head null..."); return; } record * cur = ll->head; while(cur) { printf("%s\n", cur->name ); cur = cur->next; } } // other functions
now when compile gcc on ubuntu 12.04 get:
project.c:20:15: error: unknown type name ‘record’ project.c:20:27: error: unknown type name ‘ll’ project.c:21:16: error: unknown type name ‘ll’ project.c:22:11: error: unknown type name ‘ll’ project.c:145:15: error: unknown type name ‘record’ project.c:145:27: error: unknown type name ‘ll’ project.c:162:16: error: unknown type name ‘ll’ project.c:182:11: error: unknown type name ‘ll’
how can let compiler know of struct record
, struct ll
before function headers, when structs self , declared in main()?
you can't.
the declarations must visible same level functions declared in.
move struct declarations before list function prototypes.
or add pre-declaration:
typedef struct record record;
as long list functions dealing pointers (record *
), should work.
Comments
Post a Comment