C Structure Revisited

We known that one of the unique feature of the C language is structures. They provide a method for packing together data of different types. A structure is a convenient tool for handling a group of logically related data items. It is a user define data type with a template that serves to define its data properties. Once the structure type has been defined, we can create variables of that type using declaration that are similar to the built in type declaration.For example, consider the following declaration:

struct student
{
char name[20];
int roll_number;
float total_marks;
};

The keyword struct declare student as a new data type that can hold three fields of different data types. These field are known as structure member or elements. The identifier student, which is referred to as structure name or structure tag, can be used to create variables of type student. Example:

struct student A; // C declaration

A is a variable of type student and has three member variables as defined by the template. Member variable can be accessed using the dot or period operator as fallows:

strcpy(A.name, "John");
A.roll_number = 999;
A.total_marks = 595.5;
final_total = A.total_marks + 5;
Structure can have arrays, pointer or structure as members.

No comments:

Post a Comment