How to use typedef struct tagName

Is there any special meaning of "tag" below?

typedef struct tagMSG
{
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG;


thanks
In C, a struct identifier has its own namespace, separate from the regular function and variable namespace.

Hence, in C you must use the word "struct" before structure names:
1
2
3
4
5
6
7
struct Point
{
    int x;
    int y;
};

void LineTo( struct Point pt );  // This is C 

In C++, the structure name is also placed in the regular namespace. So you could say:
1
2
3
4
5
6
7
struct Point
{
    int x;
    int y;
};

void LineTo( Point pt );  // This is C++ 

Long before C++ came along, people wanted to be able to get rid of the struct keyword also. Along to the rescue came typedef, which declares type aliases.
1
2
3
4
5
6
7
8
struct Point
{
    int x;
    int y;
};
typedef struct Point Point;

void LineTo( Point pt );  // This is C 

The two definitions can be combined:
1
2
3
4
5
6
7
8
typedef struct Point
{
    int x;
    int y;
}
Point;

void LineTo( Point pt );  // This is C 


In C/C++ hackish circles, the structure identifier (not the typedef) is called the structure's "tag". Hence, you will see constructs like
1
2
typedef struct tagMSG { ... } MSG;
typedef struct point_tag { ... } point_t;

It is not typically useful to specify the tag except when you need a pointer to the struct before it is typedefed, as in a linked list:
1
2
3
4
5
6
7
8
typedef struct llist_tag
{
    void*             data;
    struct llist_tag* next;
}
llist_t;

llist_t* head = NULL;

Hope this helps.
Topic archived. No new replies allowed.