How to Use Structures in C++

If we talk about a variable of a single type e.g. an int variable, it will only be able to store integer values. Same is the case with all other variable types such as doubles, float, long and char etc. If this is the only option we have then how can we store the data of a single person for example to store data of a student we need to store his/her id number, which should be a long, his/her name that should be a string, his/her marks that should be float value. How can we do this? Well C++ provide us with a tool called structures and they are used to build data types of your own or a structure of anything like student, employee that can hold multiple types of variables inside it. I will demonstrate the use of structures in C++ in next few steps.

Instructions

  • 1

    To create a structure, we have to define it first; the definition contains the fields that is going to store. To define a structure best way is to define it globally that is before writing your main method. Structures are defined by using following syntax.


    struct name_of_structure  {


    int x;                      // these are the fields you want to associate with a structure.


    char y;


    float z;


    };


    “struct” is the keyword that is used to define a structure and it is also used to declare a structure you have already created. Then soon after struct write name of your structure then braces. Inside the braces you have to declare the variables you want to assign to a structure e.g. float, int or long.

  • 2

    Declaring a structure you created and accessing its fields:


    Once a structure is defined, you can easily declare it just like a simple variable inside your main program. To do this you have to follow following syntax:


    struct  name_of_structure   temp;


    Once you declare a structure name temp which is of type name_of_structure then it will have three fields associated with it i.e. int x, float y and char z. To access each field individually you have to follow this syntax:


    For example to set the value of int x associated with temp use this:


    temp . x =  12;


    “.” is used to access any field associated with a structure.


    Note: if you are declaring a pointer to structure then use -> sign instead of a dot.

  • 3

    To demonstrate the use of structure more appropriately, I have written the following example. It simply creates a structure of an employee and stores his/her id, age and pay.


    #include


    using namespace std;


    struct employee {


    int id;


    int age;


    float pay;


    };



    int main()


    {


    employee xyz;    // there is now xyz employee structure with fields id, age and pay


    xyz.age = 34;      // set the age of employee


    xyz.id = 1234;    //set the id of employee


    xyz.pay = 1700.99;  // set the pay of employee


    cout<<”the pay of xyz is”<


    return 0;


    }



    Using the example above you can quickly learn the use of structures in C++

Leave a Reply

Your email address will not be published. Required fields are marked *


7 + nine =