C – Array of Structures

Home/Learn C Programming/C - Structure/C – Array of Structures
C – Array of Structures 2017-07-27T01:46:16+00:00

Array of Structure :

Like any other data type,  array of structure can be defined, so that each array element can be of structure data type.

For Example,

struct student s[100];

which defines an array called s, that contains 100 elements, each element is defined to be of type struct student.

C – ARRAY OF STRUCTURES EXAMPLE PROGRAM :

The allocation of variables in heap contains 2 parts. One is for integral data types (int and char), and the other is for float type. But it is different in case when we declare a structure.

When we define a structure with 3 data types, one is of integer, other is of character and another is of float type, the memory for all the 3 types are allocated in integral part. When we compile this type of programs we did not get any error but when we execute the program, the program terminates abnormally. This is because the link fails when connecting floating values to the structure.

To solve this problem, we have to declare 2 float values, one is of pointer type and the other is normal float value. The memory for float pointer is allocated in the integral part (because pointer has 2 bytes of unsigned int memory) and the memory for other float variable is allocated in the float part. This normal variable points to the float pointer (which resides in integral part), and thus we will established a link between integral part and float part of heap. Thus the programs executes normally.

C – ARRAY OF STRUCTURE EXAMPLE PROGRAM :

To accept student details and to calculate and display result using arrays

Passing structure as function argument :

Like any other data type a structure may also be used as function argument.

Returning structure :

 A function can  not only receive a structure as its argument, but also can return them.

 

Prev Next