Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does passing two aliases to `typedef struct` mean?

Tags:

c

struct

typedef

What does *B in the following code mean? I understand this mixture between typedef and struct. However, this *B is strange.

typedef struct Something
{
...
}
A, *B;

I saw multiple questions asking about mixing typedef with struct but non of them talked about this double definition.

like image 663
Mohammed Noureldin Avatar asked Nov 02 '25 17:11

Mohammed Noureldin


1 Answers

This is a less-common use case for the typedef keyword that allows you to define two or more type aliases in a single line. Here, this says

  • make an alias named A that represents the struct itself, and
  • make an alias named B that represents a pointer to the struct.

In that sense, it's similar to writing something like

int A, *B;

Here, this declares an integer named A and a pointer to an integer named B. The syntax here involving the * works very similarly to what's going on in the typedef statement, except that instead of introducing variables it's introducing types.

Another way to see this: this is equivalent to breaking things apart into two separate statements:

typedef struct {
   ...
} A;

typedef A* B;

Here, the first one says "A now refers to this struct type, and B now refers to a pointer to an A."

like image 158
templatetypedef Avatar answered Nov 05 '25 10:11

templatetypedef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!