I need some concept clarifications

Q1.
1
2
3
4
5
6
7
8
9
10
struct CommuPacket
{
int iPacketType;
union     
{
struct structA packetA;
struct structB packetB;
struct structC packetC;
}
};


I did not use struct that often and get confused.
Does it mean we declare a class(or structure) called CommuPacket, then inside this class, we again define further 3 more classes called structA,structB,structC and created 3 objects called packetA(and B,C)? Is it a must to add "struct" keyword in Line 6-8 in order to declare a class within a class?

Q2
1
2
3
4
typedef struct
{int a;
int b;
} A;


Does it mean we create a class/structure called A? Why do we have to missed out a name after the word struct but instead name it at the end of the code? Can I write something like this:
1
2
3
4
5
6
7
8
9
typedef struct A {
int a; 
int b;}
.
.
.
.
int main()
{A B;}


Thank you for answering.
A1:

In C, a member cannot be of the type " function return… ". That is to say, the functions cannot be member of it structures C (otherwise they would be classes). However, yes they are admitted " pointers to a function return … " (since they are variable, non-methods).

The C++ structures can include functions member (in fact they are classes), in C++ the word struct can be omitted.

A second step is to declare a variable like pertaining to the new type. In the same way that stops to declare a variable CH as of type to char we declared: to char CH; , in this case, to declare a variable st as it structures type point uses: struct Point pt; // C and C++ In C++ it is not necessary to indicate that Point is a structure (we suppose that already the compiler by the previous sentences knows to it), so that if no other variable point in the same scope of names does not exist, there is no ambiguity and it is possible to be put directly: Point pt; // C++

A2:

Probably you can't do that unless the struct is not being inicialize with an object, then that would be an Orphan Structure, look example:

1
2
3
4
5
6
7
8
9
struct Person 
{
 char name [35] 

struct Direction; // Orpahn structure

} candidate; // an instance of Person

 candidate.numero = 10;


That's all I can think of...
Last edited on
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...
Last edited on
Topic archived. No new replies allowed.