0

I'm a little confused about the structure of this struct. What is the point of typedef if you can name the struct to whatever you want without it? Is it also necessary to have " struct" in struct data_el *next when you are creating the next pointer; and wouldn't be confusing to also name that pointer the same name as the struct itself? Also, what is the point of having data_el after the bracket, when you can create a new struct in the program whenever you want, without naming an instance of it?

typedef struct data_el_{
int data;
struct data_el_ *next;
}data_el;
bursikif
  • 21
  • 4
  • It allows you to use `struct data_el_` without prefixing it with the `struct` keyword. – Irelia Apr 08 '22 at 16:46
  • Some coding standards do deprecate `typedef`ing structs, as it is hiding a potentially important detail. – Eugene Sh. Apr 08 '22 at 16:48
  • 1
    The name following the type in a `typedef` is the name of the newly defined type. In this case, `data_el` is a alias for `struct data_el_` – Gerhardh Apr 08 '22 at 16:48

1 Answers1

0

You can use struct like this:

struct data_el_ {
    int data;
    struct data_el_ *next;
};
struct data_el_ my_data_el;

But you can also typedef a struct:

typedef struct data_el_ {
    int data;
    struct data_el_ *next;
} data_el;
data_el my_data_el;

In the latter case, you can still use struct data_el_. However, inside the struct itself you must use struct data_el_.


For more information, check out the following related questions:

typedef struct vs struct definitions

Why should we typedef a struct so often in C?

User
  • 317
  • 2
  • 8