noob noob - 2 months ago 7
C Question

Is there another reason for using tags in typedef except for self referencing?

If I want to create a new struct type that contains a pointer the type itself I wouldn't be able to do

typedef struct {
my_type* p;
} my_type;


and I will need to use a tag

typedef struct my_tag {
struct my_tag* p;
} my_type;


Is there another use for the tag mechanism? because I've seen it several times and I don't understand why someone would add this to a
typedef


Also, since C and C++ are a bit different with their
struct
keyword, would that change anything if I typedef such a struct in C or in C++?

Answer

Another use is "opaque type", which is usually forward- declared as

typedef struct my_type my_type;

where you want to define a type at the same time as you do a forward declaration. typedef struct my_type; would have been nonsense. In general, you can't forward-declare without a struct tag.