Need of structure

Assume that we are going to develop a software for the college library to manage all students information.

Student information includes roll number, name, age, date of birth, and address.




Example

Need to store

Example

Data type

Roll number

624128

integer

Name

"Alice"

string

Age

20

integer

Date of birth

"1-1-2011"

string

Address

"1,New street,etc"

string

Fine

100.50

float




Can we use an array to store all the above information?

No! As we discussed earlier, an array is a homogeneous data type where we can store only similar data type.

To know more about array kindly visit this link: Need of an array

In our problem statement, we need to store different data type (integer, string, float).





What is the solution?

C language already provides the solution. That is structure.

Using structure, we can store different data type.

It is also called the heterogeneous data type.




Syntax of structure

struct name
{
    //structure members
    //can be any data type
};

Where,

struct    - keyword

name    - name of the structure

{}        - Inside braces, we should declare the structure members. It can be a combination of any data type.

;          - End of structure declaration


Example

struct student
{
    int  roll_number;
    char name[25];
    int  age;
    char dob[20];
    char address[100];
    float fine;
};


Topics You Might Like