Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
901 views
in Technique[技术] by (71.8m points)

data structures - LinkedList Struct Typedef in C

I am try to build a basic linked list in C. I seem to get an error for this code:

typedef struct
{
    char letter;
    int number;
    list_t *next;
}list_t;

char letters[] = {"ABCDEFGH"};

list_t openGame, ruyLopez;
openGame.letter = letters[4];
openGame.number = 4;
openGame.next = &ruyLopez;


ruyLopez.letter = letters[5];
ruyLopez.number = 4;
ruyLopez.next = NULL;

It won't accept my definition in the struct:

list_t *next;

And for the same reason it won't accept:

openGame.next = &ruyLopez;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you are using list_t *next in your code, the compiler doesn't know what to do with list_t, as you haven't declared it yet. Try this:

typedef struct list {
    char letter;
    int number;
    struct list *next;
} list;

As H2CO3 pointed out in the comments, using _t as an identifier suffix is not a great idea, so don't use list_t.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...