C – Structure

C – Structure 2017-07-27T01:44:46+00:00

Structure :

A structure is a user defined data type. We know that arrays can be used to represent a group of data items that belong to the same type, such as int or float. However we cannot use an array if we want to represent a collection of data items of different types using a single name. It is a convenient tool for handling a group of logically related data items.

Syntax :
struct structure_name
{
type element 1;
type element 2;
……………..
type element n;
};

In declaration the keyword struct appears first, this followed by structure name. The member should be enclosed between a pair of braces and it defines one by one each ending with a semicolon. It can also be array of structures. There is an enclosing brace at the end of declaration and it end with a semicolon.

We can declare variables as follows
struct structure_name var1,var2,…..,var n;

For Example:
To store the names, roll number and total mark of a student you can declare 3 variables. To store this data for more than one student 3 separate arrays may be declared. No memory is allocated when a structure is declared. It just defines the “form” of the structure. When a variable is made then memory is allocated. This is equivalent to saying that there’s no memory for “int” , but when we declare an integer that is. int var; only then memory is allocated. The above-mentioned case will look like

struct student
{
int rollno;
char name[25];
float totalmark;
};

We can now declare variables stud1, stud2 as follows
struct student stud1,stud2;
Thus, the stud1 and stud2 are variables of type student. The above structures can hold information of 2 students.

It is possible to combine the declaration of structure combination with that of the variables, as shown below.
struct structure_name
{
type element 1;
type element 2;
……………..
type element n;
}var1,var2,…,varn;

The following single declaration is equivalent to the two declaration presented in the previous example.
struct student
{
int rollno;
char name[25];
float totalmark;
} stud1, stud2;

The different variable types stored are called its members. The members can be accessed by using a dot (.) operator, so the dot operator is known as structuremember operator.

Example:
In the above example stud1 is a structure variable of type student. To access the member name, we would write
stud1.name
Similarly, stud1’s rollno and stud1’s totalmark can be accessed by writing
stud1.rollno                  And
stud1.totalmark

Initializing Structure Members:

Members can be initialized at declaration. This much the same manner as the element of an array; the initial value must appear in the order in which they will be assigned to their corresponding structure members,enclosed in braces and seperated by commas .The general form is

struct stucture_name  var={val1,val2,val3…..};

C – EXAMPLE PROGRAM :

Array of structures:

An array in which each element is a structure. Just as arrays of any basic type of variable are allowed, so are arrays of a given type of structures. Although structures contains many different types, the compiler never gets to know this information because it is hidden away inside a sealed structures capsule, so it can believe that all the elements in the array have the same type, even though that type is itself made up of lots of different types.

The declaration statement is given below.
struct struct_name
{
type element 1;
type element 2;
……………..
type element n;
}array name[size];

Example:
struct student
{
int rollno;
char name[25];
float totalmark;
} stud[100];

In this declaration stud is a 100-element array of structures. Hence, each element of stud is a separate structure of type student. An array of structure can be assigned initial values just as any other array. So it can hold information of 100 students.

C –  EXAMPLE PROGRAM :

Structure as structure member:

It can have one or more of its member as another structure, but a structure cannot be member to itself when a structure is used as structure member. In such situation, the declaration of the embedded structure must appear before the declaration of the outer structure. For example

 

Prev Next