×

C- STRUCTURE


In this chapter, we will understand about Structures.

What is Structure in c?

Structure in c is a user defined data type. It allows us to create a data type which can be used to group different data type in a single data type.

What is the syntax of Structure ?
struct structure_name
{
    //Structure members
}

struct is the keyword used to declare a structure. Structure members are the variables of different types.

How to declare a Structure ?
// Structure declaration
struct student
{
    int mobile;
};

Here we have created a structure of student with int data type.

How to use Structure ?
// Structure Initialization
struct student s;
s.mobile = 998; // initializing name attribute of structure
printf("%d",s.mobile); // printing value of mobile

Here we have used the structure and inserted value in mobile and then printed in console.

How to declare variable of type structure

There are two ways we can declare structure variable. Either by structure declaration or by separate declaration like any other normal types.


Example 1: structure variable declaration by structure declaration

//structure variable declaration by structure declaration
struct Student
{
    char name[50];
    char roll_number[50];
    char degree[50];
    int mobile;
} student1; // student1 is a variable of type Student

Example 2: structure variable declaration like normal data type declaration

//structure variable declaration by structure declaration
struct Student
{
    char name[50];
    char roll_number[50];
    char degree[50];
    int mobile;
};

void main()
{
    // student2 is a variable of type Student
    struct Student student2;
}

In the above 2 examples we have created two variables student1 and student2 which are of student data type


How to use structures variables

Curly braces { } are used to initialize the structure variable

File name: structure_example.c
#include<stdio.h>
struct Student
{
    char name[50];
    char roll_number[50];
    char degree[50];
    long mobile;
};

int main()
{
    struct Student student1 = {
                        "Dalpat",
                        "EC1007",
                        "MBA",
                        9876543210
                        };
    /*
        In the above initialization student1 
        is initializes as
        name assigned with value Dalpat
        roll_number with value EC1007
        degree with value MBA
        mobile with value 9876543210
    */

    printf("Student name = %s\n", student1.name);
    printf("Student roll number = %s\n",student1.roll_number);
    printf("Student degree = %s\n", student1.degree);
    printf("Student mobile = %ld\n", student1.mobile);
    return 0;
}
Output
Student name = Dalpat
Student roll number = EC1007
Student degree = MBA
Student mobile = 9876543210

Memory allocation of Structures

Memory allocated for a structure variable is equal to the sum of the memory requirements of its individual members.

Example : structure variable
struct id
{
  char color[12];
  int size;
}shirt;

Here the total number of memory allocated for structure variable shirt will be 14 bytes . A total of 12 bytes for character array and 2 bytes for integer = 14 bytes.

The advantage of using structures over union is that any member of the structure can be accessed at any time.

In the next chapter, we will learn about Unions.