开发者

Pointer to own type in struct?

开发者 https://www.devze.com 2023-01-31 06:24 出处:网络
Is it possible? typedef struct { l开发者_StackOverflow中文版istLink *next; listLink *prev; void *data;

Is it possible?

typedef struct {
    l开发者_StackOverflow中文版istLink *next;
    listLink *prev;

    void *data;
} listLink;


Yes, with this syntaxis

struct list {
    int   value;
    struct list  *next;
};

or

typedef struct list
{
      int value;
      struct list *next; 
} list;


Yes, but the typedef doesn't happen until later in the statement so you need to give the struct itself a name. E.g.

struct listLink {
    struct listLink *next;
    struct listLink *prev;

    void *data;
};

And you can wrap that in a typedef if you don't want to have to declare each instance as struct listLink <whatever>.


I prefer this version:

typedef struct listLink listLink ;

struct listLink{
    listLink *next;
    listLink *prev;

    void *data;
} ;

It clearly separates the struct from and members and variables.

Using the same name for both struct and typedef, struct listLink, listLink , is not an issue since they have separate namespaces.


This possible with th osgrx given syntax. Plus, this is called a linked list: http://www.ehow.com/how_2056292_create-linked-list-c.html

Choose a name, then use typedef to define it. Every linked list will need a structure, even if it only has one variable:

typedef struct product_data PRODUCT_DATA;

Define the structure. The last element should be a pointer to the type you just defined, and named "next":

struct product_data {
 int product_code;
 int product_size;
 PRODUCT_DATA *next;
};
0

精彩评论

暂无评论...
验证码 换一张
取 消