Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of using structure "variable" (type alias) and structure itself in C?

Tags:

c

This question might be too open, but that openness is exactly why I need to ask here.

So I'm leaning c language with an existing project, I notice someone wrote and used structs like this:

typedef struct watchpoint {
  int NO;
  struct watchpoint *next;
} WP;

// here, instead of using "struct watchpoint", they used "WP"

static WP wp_pool[NR_WP] = {};
static WP *head = NULL, *free_ = NULL;

WP* new_wp();
void free_wp(WP *wp);

Is this way of referring struct just a personal preference, or it does has special purpose? Thanks ahead.

Update:

Ah, so I missed the typedef ahead of the structure defination, quite a misleading writing style. Thanks for the comments and answers.

like image 752
boholder Avatar asked Aug 31 '25 04:08

boholder


1 Answers

It is just a syntactic difference of using typedef. WP was defined after the curly brackets, so it creates a type-alias for struct watchpoint . However, there is no difference in efficiency or runtime; it's just a way of defining type aliases. So to be concise, he defines a struct watchpoint and creates type alias WP so that instead of using struct watchpoint every time, you can use WP. Why? It's simply for readability and convenience.

You can read more about the typedef and its application here.

like image 51
ShivamQmr Avatar answered Sep 02 '25 16:09

ShivamQmr