Difference of using structure variable and structure itself as funciton return value in C?

Difference of using structure variable and structure itself as funciton return value in 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.

Answer

It is just a syntatic difference of using typedef. WP was defined in after the parenthesis, so it creates a type-alias for 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 defining struct watchpoint everytime, you can use WP. Why? It's simply for readability and convenience.

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

Enjoyed this question?

Check out more content on our blog or follow us on social media.

Browse more questions