C中的LinkedList Struct Typedef [英] LinkedList Struct Typedef in C

查看:235
本文介绍了C中的LinkedList Struct Typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  typedef我想尝试在C中构建一个基本的链接列表。 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;

它不会接受我在struct中的定义:

  list_t * next; 

由于同样的原因,它不会接受:

  openGame.next =& ruyLopez; 


解决方案

当您使用 list_t在你的代码中,下一个,编译器不知道如何处理 list_t ,因为你还没有声明。尝试这样:

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

正如H2CO3在评论中指出的,使用 _t 作为标识符后缀不是一个好主意,所以不要使用 list_t


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;

解决方案

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.

这篇关于C中的LinkedList Struct Typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆