-1

I would like to use type, which is yet not know, becuase I am using it in its own definition:

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

I do not to create new variable (which I would do, If I use tag of that struct (typedef struct foo{...} foo_t, where foo is a concrete variable) I just want to create new type and use it even in its definition. Is it possible in c?

milanHrabos
  • 2,010
  • 3
  • 11
  • 45
  • Note: there is a semicolon missing from your typedef. – wildplasser Jul 31 '20 at 11:18
  • 2
    `typedef struct foo{...} foo_t` does not "create new variable" of any kind. It defines a structure type tagged with `foo`, and aliases it to another name `foo_t`. There is nothing wrong with that (ridiculous impositions of POSIX naming restrictions notwithstanding). If you *really* wanted to you could just use `void *next;` and have the implementation code go cast/conversion crazy, but that's a dreadful idea and no one with a shred of sanity would want to maintain that code base. – WhozCraig Jul 31 '20 at 11:25
  • *ridiculous impositions of POSIX naming restrictions notwithstanding* That's a bit over-the-top. The reservation of `*_t` identifiers for POSIX is no different than the reservation of `__*` and `_[A-Z]*` identifiers by the C standard. Implementations have to reserve some portion of the singular C identifier namespace in some way. – Andrew Henle Jul 31 '20 at 12:02
  • So I have decided to use `(*void)` as it is known for compiler in the time of using it (in the struct definition) despite the `node_t` which is not know for compiler in the time of that type definition. – milanHrabos Jul 31 '20 at 12:51

2 Answers2

1

Create the struct first and define a new type based on :


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

typedef struct node node_t;
Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
0

Just declare the structure with the the tag node_t beside the typedef alias of node_t.

This provides a forward declaration of the structure node_t, which can be used for pointers to this structure type.

Note that you need to use the struct keyword at the declaration of next.

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