Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Structure

A structure is a group of variables having different or same data types.
A structure is a derived data type.

Syntax

struct<structure_name>
{
    data_type variable1;
    data_type variable2;
    data_type variable3;
        :
        :
    data_type variableN;
};

C Programming Language Structure Syntax

Example

struct Employee
{
    int id_no;
    char name[30];
    float salary;
}

Why Structure in C programming?

Suppose there are 100 employees in a certain company and that company wants to store information of those employees using their employee ID, name & salary.
Then according to the previous way of programming we would require 300 variables to store complete information of all the employees as shown below.

Example

int main()
{
    int emp1_id_no;
    char emp1_name[30];
    float emp1_salary;

    int emp2_id_no;
    char emp2_name[30];
    float emp2_salary;

    int emp3_id_no;
    char emp3_name[30];
    float emp3_salary;
      :
      :
      :
    int emp100_id_no;
    char emp100_name[30];
    float emp100_salary;
}

This type of programming creates program very big & complex resulting head ace to the programmer, which is very bad.
To overcome such problems, the structure was introduced in C programming.

Structure helps the programmer to define only one data type which is a collection of variables having different or same data types.

Therefore structure makes the program short & easy to operate.
To make use of structure, the programmer needs to declare a variable called structure variable.

Example

C Programming Language Structure Variables DeclarationHere, the structure named as Employee acts just as a template or format for storing employees’ information.
When the user defines a structure, no any space is reserved for structure in memory.
Memory for a structure is reserved only when a structure variable is declared.

For example

C Programming Language How Structure Member & Structure Variable use MemoryAbove structure requires total 58 bytes in memory for each structure variable.
But space is not reserved for structure in memory as soon as it is defined.
Space for the structure is reserved only when its variable is declared. That variable is known as a structure variable.
Suppose there are 3 structure variables declared, then each structure variable reserves 58 bytes of space in memory.
In total, it reserves 174 bytes for 3 structure variables.
Structure variable makes structure come into actual physical existence & not structure itself.
Therefore structure is just known as a template or a format or a blueprint for structure variable.

Note

Here in the above example, we have given the size of structure variable just for understanding the purpose that how the size is reserved for structure variable in memory.
Actually, the size of structure variable varies from compiler to compiler (machine to machine) due to holes present in between those bytes.
So size may be more on your machine for structure variable (including holes).
Therefore it is good to find out the size of structure variable using sizeof operator.
For better understanding, we have given an example below.

Example

#include <stdio.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
};

int main()
{
    struct Employee emp1, emp2, emp3;
    printf("\n sizeof(emp1) = %d",sizeof(emp1));
    printf("\n sizeof(emp2) = %d",sizeof(emp2));
    printf("\n sizeof(emp3) = %d",sizeof(emp3));
    printf("\n");
    printf("\n sizeof(emp1.id_no) = %d",sizeof(emp1.id_no));
    printf("\n sizeof(emp1.name) = %d",sizeof(emp1.name));
    printf("\n sizeof(emp1.salary) = %d",sizeof(emp1.salary));
    printf("\n");
    printf("\n sizeof(emp2.id_no) = %d",sizeof(emp2.id_no));
    printf("\n sizeof(emp2.name) = %d",sizeof(emp2.name));
    printf("\n sizeof(emp2.salary) = %d",sizeof(emp2.salary));
    printf("\n");
    printf("\n sizeof(emp3.id_no) = %d",sizeof(emp3.id_no));
    printf("\n sizeof(emp3.name) = %d",sizeof(emp3.name));
    printf("\n sizeof(emp3.salary) = %d",sizeof(emp3.salary));
    return 0;
}

Output

sizeof(emp1) = 60
sizeof(emp2) = 60
sizeof(emp3) = 60

sizeof(emp1.id_no) = 4
sizeof(emp1.name) = 50
sizeof(emp1.salary) = 4

sizeof(emp2.id_no) = 4
sizeof(emp2.name) = 50
sizeof(emp2.salary) = 4

sizeof(emp3.id_no) = 4
sizeof(emp3.name) = 50
sizeof(emp3.salary) = 4

Representation of above structure variables in memoryC Programming Language Structure Variable Representation in Memory
Example for better understanding of difference between structure & structure variable

C Programming Language Difference Between Structure & Structure Variable

 

Leave a reply