Question : Having trouble working with pointers in C program

I am working on a program where I need to pass nodes from point A to point B to point C.  There are 3 types of nodes (A, B, C).  In the particular function I show below, I am trying to pass Node B to location C.  Node B is to contain a Node A (hence the reason it is passed in) along with some other information.

The main part I am struggling with is how to access data elements for the structure that the pointers are pointing to.  

I am also unsure of the role "union" plays in the NODE_TYPE structure.  Does it mean I can have an A_TYPE or B_TYPE or C_TYPE?

A simplified program showing the function BtoC along with part of the .h file is attached.

Any help would be greatly appreciated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
/***** In main program ******/

....

BtoC(NODE_TYPE *node_from_a)
{
   NODE_TYPE *node_to_c; /* use node_alloc() to create this */ 
   /* THIS IS WHERE I NEED HELP */
   node_to_c = node_alloc();
   /* create b_node: use node_alloc() - THIS IS WHERE I NEED HELP */
   b_node = node_alloc(); //CORRECT?????
   /* Fill the fields of b_node - THIS IS WHERE I NEED HELP */
   b_node->u->a_node = &node_from_a; //CORRECT?????
   b_node->type = TYPE_B_NODE; //CORRECT?????

   node_to_c->u->b_node = &b_node; //????
   node_to_c->type = TYPE_C_NODE; //????
   
   /* send to c */
   send_node_to_c(node_to_c);
   
   return 0;
}

....

/***** In .h file ******/

....

/* data unit between A and B */
typedef struct {
     int  snode;                    /* source node address      */
     int  dnode;                    /* destination node address */
     char data[DATASIZE];           /* message                  */
} A_TYPE;

/* data unit between B and C */
typedef struct {
     int curr_node;   /* address of this node */
     int next_node;   /* address of next node */
     A_TYPE a_node;
     enum boolean error; /* YES or NO */
} B_TYPE;

/* data unit between C layers */
typedef struct {
     int type;
     B_TYPE b_node;
} C_TYPE;

/* Values for the type field in NODE_TYPE */
#define TYPE_A_NODE 0
#define TYPE_B_NODE 1
#define TYPE_C_NODE 2

typedef struct {
     union {
	  A_TYPE a_node;
	  B_TYPE b_node;
	  C_TYPE c_node;
     } u; 

     int type;    /* One of TYPE_A_NODE, TYPE_B_NODE, TYPE_C_NODE */
} NODE_TYPE;

int node_init(); /* Initialize the pool of nodes. Returns 0 for failure. */
NODE_TYPE *node_alloc(); /* Allocates a node and returns a pointer */

....

Answer : Having trouble working with pointers in C program

if you want to copy.. that is creating a duplicate, then

If they are pointers:
*node_to_c->u->b_node->a_node = *node_from_a->u->a_node;

if you want them to point to the same node
node_to_c->u->b_node->a_node = node_from_a->u->a_node;


If they are not pointers,, then (it will create duplicate node)
node_to_c->u.b_node.a_node =node_from_a.u.a_node;
Random Solutions  
 
programming4us programming4us