6

Is it possible?

typedef struct {
    listLink *next;
    listLink *prev;

    void *data;
} listLink;
  • The initial confusion experienced by tm1rbt is the classic argument for NOT typedeff'ing structs. Writing 'struct listLink' in client code is not so onerous. Having the typedef is obfuscatious and totally unnecessary. – William Pursell Dec 17 '10 at 13:47
  • Related: [typedef struct vs struct definitions](http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions) – styfle Jan 05 '12 at 21:10

4 Answers4

7

Yes, with this syntaxis

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

or

typedef struct list
{
      int value;
      struct list *next; 
} list;
osgx
  • 90,338
  • 53
  • 357
  • 513
3

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>.

Tommy
  • 99,986
  • 12
  • 185
  • 204
0

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.

2501
  • 25,460
  • 4
  • 47
  • 87
0

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;
};
2501
  • 25,460
  • 4
  • 47
  • 87
Oleiade
  • 6,156
  • 4
  • 30
  • 42