Arrays Structures and Unions in C, Structures and Functions

By
Last updated:

We use arrays structures to describe the format of a number of related variables. For example, in analyzing the marks obtained by a class of students, we may use a template to describe student name and marks obtained in various subjects and then declare all the students as structure variables. In such cases, we may declare an array of structures, each element of the array representing a structure variable. For example: struct class student [100]; defines an array called student, that consists of 100 elements. Each element is defined to be of the type struct class. Consider the following declaration.

struct marks
{
int subject1;
int subject2;
int subject3;
};
main()
{
struct marks student[3] =
{{45,68,81}, {75,53,69}, {57,36,71}};
This declares the student as an array of three elements student[0], student[1], and student[2] and initializes their members as follows:
student[0].subject1 = 45;
student[0].subject2 = 65;
…..
…..
student[2].subject3 = 71;
Note that the array is declared just as it would have been with any other array. Since student is an array, we use the usual array-accessing methods to access individual elements and then the member operator to access members. Remember, each element of student array is a structure variable with three members.

An array of structures is stored inside the memory in the same way as a multi-dimensional array. The student actually looks as shown in Fig.

For the student array discussed above, write a program to calculate the subject-wise and student-wise totals and store them as a part of the structure.

Arrays Structures

The program is shown in Fig. We have declared a four-member structure, the fourth one for keeping the student totals. We have also declared an array total to keep the subject totals and the grand-total. The grand total is given by total.total. Note that a member name can be any valid C name and can be the same as an existing structure variable name. The linked name total.total represents the total member of the structure variable total.

student[0].subject 1 45
.subject 2 68
.subject 3 81
student[1].subject 1 75
.subject 2 53
.subject 3 69
student[2].subject 1 57
.subject 2 36
.subject 3 71

 

Structures and Functions

We know that the main philosophy of C language is the use of functions. And therefore, it is natural that C supports the passing of structure values as arguments to functions. There are three methods by which the values of a structure can be transferred from one function to another.

  1. he first method is to pass each member of the structure as an actual argument of the function call. The actual arguments are then treated independently like ordinary variables. This is the most elementary method and becomes unmanageable and inefficient when the structure size is large.
  2. The second method involves passing of a copy of the entire structure to the called function. Since the function is working
    on a copy of the structure, any changes to structure members within the function are not reflected in the original structure (in the calling function). It is, therefore, necessary for the function to return the entire structure back to the calling function. All compilers may not support this method of passing the entire structure as a parameter.
  3. The third approach employs concept called pointers to pass the structure as an argument. In this case, the address location of the structure is passed to the called function. The function can access indirectly the entire structure and work on it. This is similar to the way arrays are passed to function. This method is more efficient as compared to the second one.

In this section, we discuss in detail the second method, while the third approach using pointers is discussed in the next chapter, where pointers are dealt in detail.

The general format of sending a copy of a structure to the called function is: function_name (structure_variable_name); The called function takes the following form:

data_type function_name(struct_type st_name)
{
…..
…..
return(expression);
}

The following points are important to note:

  1. The called function must be declared for its type, appropriate to the data type it is expected to return. For example, if it is returning a copy of the entire structure, then it must be declared as struct with an appropriate tag name.
  2. The structure variable used as the actual argument and the corresponding formal argument in the called function must be of the same struct type.
  3. The return statement is necessary only when the function is returning some data back to the calling function. The expression may be any simple variable or structure variable or an expression using simple variables.
  4. When a function returns a structure, it must be assigned to a structure of identical type in the calling function.
  5. The called functions must be declared in the calling function appropriately.

Unions

Unions are a concept borrowed from structures and therefore follow the same syntax as structures. However, there is major distinction between them in terms of storage. In structures, each member has its own storage location, whereas all the members of a union use the same location. This implies that, although a union may contain many members of different types, it can handle only one member at a time. Like arrays structures, a union can be declared using the keyword union as follows:

union item
{
int m;
float x;
char c;
} code;

This declares a variable code of type union item. The union contains three members, each with a different data type. However, we can use only one of them at a time. This is due to the fact that only one location is allocated for a union variable, irrespective of its ize.

The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union. In the declaration above, the member x requires 4 bytes which is the largest among the members. Figure shows how all the three variables share the same address. This assumes that a float variable requires 4 bytes of storage.

To access a union member, we can use the same syntax that we use for structure members. That is,
code.m
code.x
code.c
are all valid member variables. During accessing, we should make sure that we are accessing the member whose value is currently stored. For example, the statements such as
code.m = 379;
code.x = 7859.36;
printf(“%d”, code.m);
would produce erroneous output (which is machine dependent).

In effect, a union creates a storage location that can be used by any one of its members at a time. When a different member is assigned a new value, the new value supersedes the previous member’s value.

Unions may be used in all places where a structure is allowed. The notation for accessing a union member which is nested inside a structure remains the same as for the nested structures.

Unions may be initialized when the variable is declared. But, unlike arrays structures, it can be initialized only with a value of the same type as the first union member. For example, with the preceding, the declaration union item abc = {100}; is valid but the declaration union item abc = {10.75}; is invalid. This is because the type of the first member is int. Other members can be initialized by either assigning values or reading from the keyboard.

Size of Structures – Arrays of Structures

We normally use structures, unions, and arrays to create variables of large sizes. The actual size of these variables in terms of bytes may change from machine to machine. We may use the unary operator size of to tell us the size of a structure (or any variable). The expression sizeof(struct x) will evaluate the number of bytes required to hold all the members of the structure x. If y is a simple structure variable of type struct x, then the expression sizeof(y) would also give the same answer. However, if y is an array variable of type struct x, then sizeof(y) would give the total number of bytes the array y requires.

This kind of information would be useful to determine the number of records in a database. For example, the expression sizeof(y)/sizeof(x) would give the number of elements in the array y.

Read More Topics
Operating system structures
Accessing structure members in C
Glossary for Computer Network
The Point-to-Point Protocol (PPP)
Organisation of the network layer
The TCP/IP Reference model

Santhakumar Raja

Hello The goal of this blog is to keep students informed about developments in the field of education. encourages pupils to improve as writers and readers.

For Feedback - techactive6@gmail.com

Leave a Comment