2010-10-28 11 views
20

otrzymuję ten błąd podczas kompilacji tej .c pliku źródłowegorozmiar przechowywanie „names” nie jest znana

/INIT_SOURCE_BUILD/src/names_list.c:7: error: storage size of ‘names’ isn’t known

#include <stdio.h> 
#include "list.h" 

int main(){ 

    struct List names; 
    names->size = 3; 

    struct ListElmt michael; 
    struct ListElmt john; 
    struct ListElmt adams; 

    names->head = michael; 

    michael->data = 12; 
    michael->next = john; 
    john->data = 14; 
    john->next = adams; 
    adams->data = 16; 

    struct ListElmt pointer = List->head; 
    for(int x = 0; x < 3 ; x++){ 
     printf("Iteration.%d data: %d", x, pointer->data); 
     pointer->next = pointer->next->next; 
    } 
} 

A oto nagłówek tego połączonej listy

#ifndef LIST_H 
#define LIST_H 

#include <stdio.h> 

/*          Define linked list elements*/ 

typedef struct _ListElmt{ 

void    *data; 
struct _ListElmt  *next; 

} ListElmt; 

/*          Define a structure for the list*/ 

typedef struct _List{ 

int     size; 
int     (*match)(const void *key1, const void *key2); 
void    (*destroy)(void *data); 

ListElmt    *head; 
ListElmt    *tail; 

} List; 

void list_init(List *list, void (*destroy)(void *data)); 

void list_destroy(List *list); 

int list_ins_next(List *list, ListElmt *element, const void *data); 

int list_rem_next(List *list, ListElmt *element, void **data); 

int list_size(const List *list); 

ListElmt *list_head(const List *list); 

ListElmt *list_tail(const List *list); 

int list_is_head(const ListElmt *element); 

int list_is_tail(const ListElmt *element); 

void *list_data(const ListElmt *element); 

ListElmt *list_next(const ListElmt *element); 
#endif 

Odpowiedz

36

Kiedy typedefstruct tak, że nie trzeba używać struct podczas deklarowania go:

List names; 

zamiast

struct List names; 

Nie jest również wskaźnik, więc names->size powinny być names.size.

+4

+1 Tylko komentarz, aby uczynić go jaśniejszym: 'struct List' nie istnieje. Istnieje 'struct _List' i typedef' List', który identyfikuje ten sam typ co 'struct _List'. * (Nienawidzę typedefs!) * – pmg

+0

Mam inny błąd names_list.c: 14: error: nieprawidłowy typ argumentu '->' (ma 'List') –

+0

@Sam: wskaźniki biorą skrót '->' aby uzyskać dostęp do członków , proste struktury uzyskują do nich dostęp za pomocą '.'. – pmg

3

struct List names; nie deklaruje wskaźnika, ale próbujesz go usunąć (używając ->). Zamiast tego użyj names.size.

3

Struktura jest nazywana _List. Typedef to List. Więc chcesz

List names; 

lub

struct _List names; /* probably not, the _ is convention for internal names */ 

linia jest uznającej "Lista struct", który nie został jeszcze określony.

Inne odpowiedzi są całkiem poprawne w odniesieniu do. vs -> issue

Powiązane problemy