Structures and Unions

December 24th, 2009 by Kevin | No Comments | Filed in C Programming

Structure

A structure in a C program is a user defined datatype which can be used to group together elements with different types but sharing a logical bonding between themselves.

struct Employee_
{
   int id;
   char* fname;
   char* lname;
   unsigned short age;
   char* address;
};

Here structure name is Employee_ with id, fname, lname, age, address as it’s members. A user defined datatype can be used as any existing datatype in a C program. So to declare a variable of this structure:

struct Employee_ emp;

Structures are heavily used in many of the projects using C. There is an even more convenient and better way which is used to declare and define structure data types and variables. This is by using the “typedef”.

typedef struct Employee_
{
   int id;
   char* fname;
   char* lname;
   unsigned char age;
   char* address;
}Employee;

Now we can declare a variable of this user defined datatype as:

Employee emp;

which looks very natural to declaring variables with built in data types. Note that we can also have arrays of user defined data types. To access the members of the structure, we use the dot or “.” operator. So to access the id, name, etc of the Employee structure

   emp.id = 1;
   emp.fname = "kevin";
   emp.lname = "rodrigues";
   printf("Name: %s %s\n", emp.fname, emp.lname);

We can also declare a pointer to a structure. This is quite useful when passing a structure into a function. Good programming practice suggests that we pass into a function only those arguments that we require. Sometimes the list of arguments can be quite large. So we can group them together in a structure and pass that structure instead. However a structure is passed call by value which will create a copy of it. Instead, we can just pass a pointer to that structure thus saving significant overhead and increasing program efficiency. To access the members of a structure using a pointer, we use the “->” or arrow operator.

void printRecord(Employee *pEmp)
{
   printf("Name: %s %s\n", pEmp->fname, pEmp->lname);
   printf("Age: %d\n", pEmp->age);
   printf("Address: %s\n", pEmp->address);
}

Self referential structures

One of the most important usage of structures in creating a self referential structure for use in data structures like link lists and trees. While it is illegal to use a structure declaration within itself, we can use a pointer to a structure as it’s own member.

typedef struct ListNode_   /* node of a link list */
{
   void* data;
   ListNode* pNext;
}ListNode;

typedef TreeNode_   /* node of a binary tree */
{
   void* data;
   TreeNode* pLeft;
   TreeNode* pRight;
}TreeNode;

Union

A union is also a user defined data type with logical members together. What makes it different from a structure, it that it can only hold value for one member at a time. The size of a union is set to the size of the largest member that it can hold. Suppose I want to print either an error code or an error message. Using a union as below

typedef union PrintVar_
{
   int ivar;
   float fvar;
   char* chrvar;
}PrintVar;

The members of a union can be accesssed similar to those of structures using a dot operator or with a pointer to a union using a “->” operator

   Printvar prnVar;
   Printvar *pVar;

   pVar = &prnVar;

   prnVar.ivar = 10;
   printf("ivar = %d\n", prnVar.ivar);
   prnVar.fvar = 3.14;
   printf("fvar = %d\n", pVar->fvar);

   pVar->chrvar = "hello";
   printf("chrvar = %s\n", prnVar.chrvar);

It is important to remember that at a time only one member of a union can be accessed because they share the same memory. So the last member which was assigned a value represents the current value present in the union variable. I have not been able to read projects using unions for their functionality.

Do you have any good examples of using unions which you could share with us?
 

Related Posts:

Tags: ,