Your questions are related to plain-old C, not C++, but the following generally applies to both.
A1
A struct - type declaration can stand by itself:
1 2 3 4
|
struct IntPair
{
int left, right;
};
| |
Normally, however, a type name is part of a data declaration, which always has the form:
type name;
where a new variable named
name of type
type is declared.
In the case of
CommuPacket, the struct is declared by itself -- naming a new type for the compiler. However, within that struct are three members whose types are
previously declared types: "
struct structA", "
struct structB", and "
struct structC".
Keep reading for that to make sense.
A2
The
typedef keyword introduces an alias for another type, and is always declared thus:
typedef typename alias;
A
struct has a
tag (or name) that exists in a separate namespace for
structs. However, it is often more convenient to refer to the struct type without having to type the "
struct" keyword -- this is done via the aforementioned
typedef. Examine how the two work in the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
struct node_tag
{
int data;
struct node_tag* next; /* so far, all that is defined is the "struct node_tag" type */
};
typedef node_tag node_t; /* now we have a type alias where "node_t" == "struct node_tag" */
void print_list( node_t* head ) /* an example of use */
{
node_t* node = head; /* we could have said "struct node_tag* node = head;" instead */
while (node)
{
printf( "%d\n", node->data );
node = node->next;
}
}
| |
So, the reason the last example you gave doesn't work is because it breaks the structure. You are trying to make a type alias for "
struct A" (which presumably doesn't exist yet) and the alias is a compound statement instead of an identifier (a name).
Hope this helps.
[edit] man, always too slow...