Structures


STRUCTURES

A structure is a collection of variables of similar or different data type which are grouped together. Its format is defined by the programmer e.g a structure could consist of the employee’s name, department number and designation.

Steps to Declare and Access Structure


  1. Declaring a structure type


    Data types such as int, double and float are predefined by the compiler, but structure is collection of number of items of similar or different data type therefore one should use struct statement to tell the compiler.

    struct easy
    {
    int number;
    float X;
    };

  2. Defining structure variable


    After declaring structure programmer can define its variable by using  name of  structure.
    Example:
    easy E;
    Here easy is new data type(structure) and E is variable name.

  3. Accessing member of structure


    To access the member items (declared in structure) structure uses an approach called dot operator also known as Membership operator.
    Example:
    E.number;

    E.X;
     
    Here ‘E’ is the structure and ‘number’ is its member.The dot operator (.) connects a structure variable name with a member of the structure.



Example


#include<iostream.h>

#include<conio.h>

int main()

{

struct first

{

int number;

float X ;

};

struct first E;

E.number=2;

E.X=3.1416;

cout<<“E.number= ” <<E.number<<endl<<”E.X= ”<<E.X;

}