Broad Network


Structure and Union in the C Computer Language

Part 24 of the Complete C Course

Foreword: Structure and Union in the C Computer Language

By: Chrysanthus Date Published: 11 Jun 2024

Introduction

The char type, the integer type (and its variants), the float type, the double type, and the pointer type, form a larger category called the scalar type. Arrays and structures form another larger category, called the aggregate type.

Reasons for having the Structure Type
An array is a set of objects of the same type. There are times when a set of objects of different types are needed. The credentials of a man form a set of objects of different types. The credentials of a man can consist of his name, his age, his salary and his highest qualification. His name e.g. "John Smith", is a string. His age can be considered as an integer if the fractional year is not considered important. His salary (per week) is a float, because it may have some decimal digits, e.g. $1425.45. His highest qualification is a string. The reserved word for a structure in C is struct. Consider the following program:

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        struct {char *name; int age; float salary; char *HQ;} employee1;

        employee1.name = "John Smith";
        employee1.age = 30;
        employee1.salary = 1234.12;
        employee1.HQ = "Msc";

        printf("%s\n", employee1.name);
        printf("%i\n", employee1.age);
        printf("%f\n", employee1.salary);
        printf("%s\n", employee1.HQ);

        return 0;
    }

The output is:

    John Smith
    30
    1234.119995
    Msc

The first statement in the main() function begins with the reserved word, "struct". After that, the members of the struct are declared in braces. The last semicolon in the braces must be there. In this situation, no member has been assigned its value. employee1 identifies a struct object to which members of the struct will be assigned values. In other-words, the name of a possible struct object is employee1. Put another way, one of the employees of a set of employees, is employee1. The credentials of employee1 are different from the credentials of other employees (employee2, employee3, employee4, etc.). Note how the string members have been declared this time, without the reserved word, const. This means that the pointer to the first character of each string (array), will not be constant. The code segment after, assigns values for the members of employee1. The printf() code segment prints out the member values. The decimal part of the float member, was printed approximately, and with extra decimal digits. How to round the extra digits, will be explained later in this chapter. Note how a member is accessed outside the struct, using the dot operator, '.' .

With the array, the values of the array object are of the same type, and placed in memory, next to one another. With the struct, the values of the object (e.g. employee1) are of different types, but still placed in memory, one next to the other.

Tag

In a company, there are more than one accountant; there are more than one secretary; there are more than one manager; etc. The set of accountants form the class of accountants, which is a type of employees. The set of secretaries form the class of secretaries, which is a type of employees. The set of managers form the class of managers, which is a type of employees; and so on. This class (or type) is called a tag, in C.

Different accountants, of the class of accountants, have different credentials. Different secretaries, of the class of secretaries, have different credentials. Different managers, of the class of managers, have different credentials. In order to have a struct from which different sets of credential values associated with different accountants (employees) can be produced, the struct declaration for accountants, needs to have the accountants tag, as in:

    struct Accountants {char *name; int age; float salary; char *HQ;};

which is the declaration of the tag (class) of accountants. Note the position and name of the tag, Accountants. There is no text between the closing '}' and ';' for now. From the Accountants struct, different sets of credential struct objects, can be created. The objects can be given names like, employee1, employee2, employee3, etc. In order to create an object such as employee1, from the Accountants struct tag, use the statement:

        struct Accountants employee1;

Note that use of the reserved word, struct, at the beginning of the expression (case sensitive). This whole expression is also a statement, because it ends with a semicolon. It is also a declaration (not definition). To assign the credential values for employee 1, do:

        employee1.name = "John Smith";
        employee1.age = 30;
        employee1.salary = 1234.12;
        employee1.HQ = "Msc";

Another employee object identified as employee2, is created from the Accountants struct tag as:

        struct Accountants employee2;

The values for employee2 can be assigned as follows:

        employee2.name = "Mary Jones";
        employee2.age = 35;
        employee2.salary = 3856.49;
        employee2.HQ = "Phd";

The identifiers for the objects can be introduced, along side the struct tag declaration, as follows:

        struct Accountants {char *name; int age; float salary; char *HQ;} employee1, employee2, employee3;

The object names, employee1, employee2, employee3, etc. are separated by commas. This is indirect declaration for employee1, employee2, employee3, etc. In this case, there is no need for the declaration of employee1, employee2, employee3, etc. again, as indicated above.

Note: after declaring the struct with the tag, as in,

    struct Accountants {char *name; int age; float salary; char *HQ;}

employee1 can be defined as follows:

    struct Accountants employee1 = {"John Smith", 30, 1234.12, "Msc"};    

Union

Union is the same as struct, except that there is overlapping of all the objects. The following program illustrates this:

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        union U {char arr[5]; char ch;};    

        union U u;

        u.arr[0] = 'A'; u.arr[1] = 'B'; u.arr[2] = 'C'; u.arr[3] = 'D'; u.arr[4] = 'E';
        printf("%c %c %c %c %c \n", u.arr[0], u.arr[1], u.arr[2], u.arr[3], u.arr[4]);
        
        u.ch = 'Z';
        printf("%c %c %c %c %c \n", u.arr[0], u.arr[1], u.arr[2], u.arr[3], u.arr[4]);        

        return 0;
    }

The output is:

    A B C D E
    Z B C D E

In the main() function, the first line declares the union, U. The second line creates (instantiates) an object for the union, u. The first member for the union object is an array. It consists of the characters 'A', 'B', 'C', 'D', 'E' next to one another in memory. This is not a string, because it does not end with '\0'. The first printf() function call from the stdio library, displays the five characters of the union member array object.

In the definition of the union object, the second member object is just a character, 'Z'. As the second member, 'Z' was included into the whole union object, it replaced the first character of the array object - that is overlapping. And so the second printf() function, prints 'Z' as the first character of the array object.

Pointer to a Struct or Union
It is possible to have a pointer to a struct. The pointer is to the struct object and not to the tag class. The following program illustrates this:

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        struct Accountants {char *name; int age; float salary; char *HQ;} employee1;

        struct Accountants * emp1 = &employee1;

        emp1->name = "John Smith";
        emp1->age = 30;
        emp1->salary = 1234.12;
        emp1->HQ = "Msc";

        printf("%s\n", emp1->name);
        printf("%i\n", emp1->age);
        printf("%f\n", emp1->salary);
        printf("%s\n", emp1->HQ);

        return 0;
    }

The declaration of the struct has its tag (class) name and one object name, employee1. The second line in the main() function creates the pointer to the struct object, in the same way that a pointer to an int object or float object is created. The pointer struct object and the pointed struct object have two different names. When accessing the struct members, the arrow operator, -> instead of the dot operator, is used. The output is:

    John Smith
    30
    1234.119995
    Msc

The pointer to a union object is created and used in the same way.



Related Links

More Related Links

Cousins

BACK NEXT

Comments