× Back Structure definition Declaration Initialization Accessing structure member Copying and comparing structure variable Storage of structure in memory Arrays within structures Array of structure Structures within structure Practise program Structure as a function argument Structure Padding and Packing typedef Practise program 2
Next Topic →

Structure

Definition

                   
struct tagname{
    datatype member1;
    datatype member2;
    ...
    ...
    datatype memberN;
};
                   
               

Example ↓

                   
struct student
{
    char name[20];
    int rollno;
    float marks;
}
                   
               

Declaring Structure Variables

With structure definition

                           
struct student
{
    char name[20];
    int rollno;
    float marks;
} stu1, stu2, stu3;
                       
                   
  • Here stu1, stu2 and stu3 are variables of type struct student.
  • When we declare a variable while defining the structure template, the tagname is optional. So we can also declare them as ↓
                           
struct 
{
    char name[20];
    int rollno;
    float marks;
} stu1, stu2, stu3;
                       
                   
  • If we declare variables in this way, then we'll not be able to declare other variables of this structure type anywhere else in the program nor can we send these structure variables to functions.

Using structure tag

                           
struct student 
{
    char name[20];
    int rollno;
    float marks;
};

struct student stu1, stu2;
struct student stu3;
                       
                   

Initialization of structure variables

Compile time initialization

                        
#include<stdio.h>
#include<string.h>

struct student
{
    char name[20];
    int rollno;
    float marks;
};

int main()
{
    struct student s1 = {"Harry", 23, 45};
    struct student s2;
    // s2.name = "Jerry"; // This is invalid syntax 
    strycpy(s2.name, "Jerry"); // This is correct 
    //  when working with a struct that contains a character array as a member, you cannot use the array initialization syntax to initialize the member directly.
    // Instead, you need to copy the value of the string literal to the member using a function like strcpy().
    s2.rollno = 23;
    s2.marks = 44;
    return 0;
}
                       
                   

Runtime initialization

                           
#include<stdio.h>

struct student
{
    char name[20];
    int rollno;
    float marks;
};

int main()
{
    struct student s1;
    printf("Enter name of student : ");
    scanf("%s", s1.name);
    printf("Enter roll number of student : ");
    scanf("%d", &s1.rollno);
    printf("Enter marks of student : ");
    scanf("%f", &s1.marks);
    return 0;
}
                       
                   

Accessing structure member

                     
#include<stdio.h>

struct student
{
    char name[20];
    int rollno;
    float marks;
};

int main()
{
    struct student s1;
    printf("Enter name of student : ");
    scanf("%s", s1.name);
    printf("Enter roll number of student : ");
    scanf("%d", &s1.rollno);
    printf("Enter marks of student : ");
    scanf("%f", &s1.marks);

    printf("Enter name of student : %s", s1.name);
    printf("Enter roll number of student : %d", s1.rollno);
    printf("Enter marks of student : %f", s1.marks);

    return 0;
}
                    
                

Copying and comparing structure variables

Program for comparision of structure variables ↓

                   
#include <stdio.h>
#include <string.h>

struct person {
    char name[50];
    int age;
    float height;
};

int compare_persons(struct person p1, struct person p2) {
    if (strcmp(p1.name, p2.name) == 0 && p1.age == p2.age && p1.height == p2.height) { //strcmp returns 0 if both the input are same
        return 1;
    } else {
        return 0;
    }
}

int main() {
    struct person p1 = {"John", 30, 1.75};
    struct person p2 = {"Mary", 25, 1.60};

    if (compare_persons(p1, p2)) {
        printf("The two persons are the same.\n");
    } else {
        printf("The two persons are different.\n");
    }

    return 0;
}
                   
               
                    
#include <stdio.h>
struct number
{
    int a, b, large;
};
int main()
{
    struct number x;
    printf("Enter first number : ");
    scanf("%d", &x.a);
    printf("Enter second number : ");
    scanf("%d", &x.b);
    if (x.a > x.b)
    {
        x.large = x.a;
        printf("%d is larger number\n", x.large);
    }
    else if (x.b > x.a)
    {
        x.large = x.b;
        printf("%d is larger number\n", x.large);
    }
    else
    {
        printf("Both number are equal");
    }
    return 0;
}
                    
                

Storage of Structure in Memory

                   
// Program to show that members of structure are stored in consecutive memory locations 
#include<stdio.h>
int main()
{
    struct student
    {
        char name[5];
        int roll;
        float marks;
    }stu;
    printf("Address of name = %u\n", stu.name);
    printf("Address of roll = %u\n", &stu.roll);
    printf("Address of marks = %u\n", &stu.marks);

    /*
        Address of name = 3192890648 // 8 
        Address of roll = 3192890656 // 4
        Address of marks = 3192890660 // 4
    */
    return 0;
}
                   
               

Arrays within structures

                   
struct student
{
    char name[20];
}
                   
               
                   
struct student
{
    char name[20];
    int rollno;
    int submarks[4];
};
                   
               

Array of structure

                   
struct student
{
    int roll;
    char name[50];
    float marks;
}s[10];
                   
               
                   
struct student s1 = {1, "Ajay", 75.5};
struct student s2[3] = {{1, "Ajay", 75.5}, {2, "Vijay", 76.5}, {3, "Akash", 65.5}};
                   
               
                     
#include <stdio.h>
struct Student
{
    char name[20];
    int rollNo, id;
    float fees;
};
int main()
{
    int n;
    struct Student many[100];
    printf("Enter the number of student you want : ");
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        printf("\n||||| Enter Detail of student : %d |||||\n", i + 1);
        printf("Name : ");
        scanf("%s", many[i].name);
        printf("\nRoll Number : ");
        scanf("%d", &many[i].rollNo);
        printf("\nFees value : ");
        scanf("%f", &many[i].fees);
        printf("\nID : ");
        scanf("%d", &many[i].id);
    }
    printf("\n_-_-_-_-_-_ Displaying all the student's details _-_-_-_-_-_\n");
    for (int i = 0; i < n; i++)
    {
        printf("\n\n||||| Detail of student : %d |||||\n", i + 1);
        printf("Name : %s", many[i].name);
        printf("\nRoll Number : %d", many[i].rollNo);
        printf("\nFees value : %.1f", many[i].fees);
        printf("\nID : %d", many[i].id);
    }
    return 0;
}
                    
                

Structures within Structure (Nested Structure)

                   
#include <stdio.h>
struct date
{
    int d, m, y;
};
struct employee
{
    int id;
    struct date dob;
    struct date doj;
} e1;
int main()
{
    printf("Enter id of a employee : ");
    scanf("%d", &e1.id);
    printf("Enter date of birth : ");
    scanf("%d%d%d", &e1.dob.d, &e1.dob.m, &e1.dob.y);
    printf("Enter date of joining : ");
    scanf("%d%d%d", &e1.doj.d, &e1.doj.m, &e1.doj.y);

    printf("ID: %d, DOB: %d/ %d/ %d and DOJ: %d/ %d/ %d", e1.id, e1.dob.d, e1.dob.m, e1.dob.y, e1.doj.d, e1.doj.m, e1.doj.y);
    return 0;
}
// In this example we also define the structure date inside the definition of structure employee.
                   
               
                   
#include <stdio.h>

// Define a structure for a point in 2D space
struct point {
    int x;
    int y;
};

// Define a structure for a rectangle
struct rectangle {
    struct point top_left;
    struct point bottom_right;
};

int main() {
    // Create a rectangle with top-left corner at (1, 3) and bottom-right corner at (4, 2)
    struct rectangle r = {{1, 3}, {4, 2}};

    // Print the coordinates of the top-left and bottom-right corners of the rectangle
    printf("Top-left corner: (%d, %d)\n", r.top_left.x, r.top_left.y);
    printf("Bottom-right corner: (%d, %d)\n", r.bottom_right.x, r.bottom_right.y);

    return 0;
}
                   
               
                   
#include <stdio.h>

// Define a structure for a date
struct date {
    int day;
    int month;
    int year;
};

// Define a structure for a person
struct person {
    char name[20];
    struct date birthday;
};

int main() {
    // Create a person with name "Alice" and birthday on December 3, 1995
    struct person p = {"Alice", {3, 12, 1995}};

    // Print the name and birthday of the person
    printf("Name: %s\n", p.name);
    printf("Birthday: %d/%d/%d\n", p.birthday.day, p.birthday.month, p.birthday.year);

    return 0;
}
                   
               

Practise

                     
#include <stdio.h>

struct distance
{
    int k, m, c;
};

int main()
{
    struct distance d1, d2, sum;
    for (int i = 1; i <= 2; i++)
    {
        printf("Enter the %d distance in Kilometers : ", i);
        scanf("%d", (i == 1) ? &d1.k : &d2.k);
        printf("Enter the %d distance in meters : ", i);
        scanf("%d", (i == 1) ? &d1.m : &d2.m);
        printf("Enter the %d distance in centimeters : ", i);
        scanf("%d", (i == 1) ? &d1.c : &d2.c);
    }
    sum.k = d1.k + d2.k;
    sum.m = d1.m + d2.m;
    sum.c = d1.c + d2.c;
    if (sum.c >= 100)
    {
        int times = sum.c / 100;
        for (int i = 0; i < times; i++)
        {
            sum.m++;
        }
        sum.c = sum.c % 100;
    }
    if (sum.m >= 1000)
    {
        int times = sum.m / 1000;
        for (int i = 0; i < times; i++)
        {
            sum.k++;
        }
        sum.m = sum.m % 1000;
    }
    printf("Sum distance KM : %d\n", sum.k);
    printf("Sum distance M : %d\n", sum.m);
    printf("Sum distance CM : %d\n", sum.c);
    return 0;
}
                    
                
                       
#include <stdio.h>
#include <stdlib.h>
struct Complex
{
    int real, imag;
} no1, no2, sum;
void enterComp();
void display();
void sumComp();
void sub();
int main()
{
    int option;
    while (1)
    {
        printf("\n\n\t\t\t\t  -_-_-_-_- MENU -_-_-_-_-  \n\n\t\t\t\t1. Input two numbers,\n\t\t\t\t2. Display two numbers,\n\t\t\t\t3. Addition of two complex number,\n\t\t\t\t4. Subtraction of two complex number,\n\t\t\t\t5. Exit\n\n");
        printf("\t\t\t\tEnter your option : ");
        scanf("%d", &option);

        switch (option)
        {
        case 1:
            enterComp();
            break;
        case 2:
            display();
            break;
        case 3:
            sumComp();
            break;
        case 4:
            sub();
            break;
        case 5:
            exit(0);
        default:
            printf("\n\t\t\t\t** ** Enter correct option ** **");
        }
    }
    return 0;
}
void enterComp()
{
    printf("\n\t\t\t\tEnter Real value of first complex number : ");
    scanf("%d", &no1.real);
    printf("\n\t\t\t\tEnter Imaginary value of first complex number : ");
    scanf("%d", &no1.imag);
    printf("\n\t\t\t\tEnter Real value of second complex number : ");
    scanf("%d", &no2.real);
    printf("\n\t\t\t\tEnter Imaginary value of second complex number : ");
    scanf("%d", &no2.imag);
}
void display()
{
    printf("\n\t\t\t\tFirst complex number : %d + %di", no1.real, no1.imag);
    printf("\n\t\t\t\tSecond complex number : %d + %di", no2.real, no2.imag);
}
void sumComp()
{
    printf("\n\t\t\t\tThe sum is %d + %di ", no1.real + no2.real, no1.imag + no2.imag);
}
void sub()
{
    printf("\n\t\t\t\tThe subtraction is %d + %di ", no1.real - no2.real, no1.imag - no2.imag);
}
                     
                 

Structure as a function argument

                   
#include <stdio.h>

struct Rect
{
    int l, b;
};
struct Tri
{
    int a, b, c;
};
int rectPara(struct Rect);
int triPara(struct Tri);
int main()
{
    struct Rect rectangle1 = {5, 10};
    struct Tri triangle1 = {4, 6, 10};
    printf("The parameter of rectangle is : %d\n", rectPara(rectangle1));
    printf("The parameter of triangle is : %d\n", triPara(triangle1));
    return 0;
}
int rectPara(struct Rect a)
{
    return 2 * (a.l + a.b);
}
int triPara(struct Tri x)
{
    return (x.a + x.b + x.c);
}
                   
               

Output ↓

                   
The parameter of rectangle is : 30
The parameter of triangle is : 20
                   
               

Structure Padding and Packing

Concept of Structure Padding

  • Before that, we should know how memory is allocated to structure members?
    • When an object of some structure type is declared then some contiguous block of memory will be allocated to structure members.
    • For example:
                                          
      struct abc {
          char a;
          char b;
          int c;
      } var;
                                          
                                      
      When the variable of structure type will be created then the memory will be allocated.
  • Calculating the size of the struct
    • For example:
                                                               
      // let the size of int is 4 bytes and size of char is 1 byte.
      
      struct abc {
          char a; // 1 byte
          char b; // 1 byte 
          int c; // 4 bytes
      } var;
      
      // total = 6 bytes
      // but this is wrong 
                                          
                                      
  • There is a concept called structure padding.
    • Processor doesn't read 1 byte at a time from memory.
    • It reads 1 word at a time.
    • If we have a 32 bit processor then it means it can access 4 bytes at a time which means word size is 4 bytes.
    • If we have a 64 bit processor then it means it an access 8 bytes at a time which means word size is 8 bytes.
  • When we define a structure in C programming language, the compiler may add some extra bytes between the members of the structure to align them properly in memory. This is called structure padding. The reason for structure padding is to improve performance by reducing the number of memory accesses needed to access the structure members.
  • Note: structure padding is performed by the compiler by default in most cases.
                           
#include <stdio.h>
struct abc {
    char a; // 1 byte
    char b; // 1 byte
    int c; // 4 bytes
};

int main()
{
    struct abc var;
    printf("%d", sizeof(var));
    return 0;
}
                       
                   
  • Output of above program is 8.

But what happens if we change the order of members? Does it affects the size?

                             
#include <stdio.h>
struct abc {
    char a; // 1 byte
    int b; // 4 bytes
    char c; // 1 byte
};

int main()
{
    struct abc var;
    printf("%d", sizeof(var));
    return 0;
}
                        
                    
  • Now the output is 12.

Structure packing

  • Because of structure padding, size of structure becomes more than the size of the actual structure. Due to this some memory will get wasted.
  • Structure packing is the opposite of structure padding. It is the process of minimizing the amount of memory used by a structure by removing the padding between its members. This can be useful in certain cases, such as when working with embedded systems where memory usage is critical.
  • We can avoid the wasatage of memory by simply writing #pragma pack(1)
                           
#include <stdio.h>
#pragma pack(1)

struct abc{
    char a;
    int b;
    char c;
} var;

int main()
{
    printf("%d", sizeof(var)); // 6
    return 0;
}
                       
                   
  • #pragma is a special purpose directive used to turn on or off certain features.

typedef Keyword

                   
typedef existing_data_type new_data_type_name;
                   
               
                   
typedef int my_integer;
                   
               

Use of typedef in structures

                           
// structure without using typedef 
struct car {
    char engine[50];
    char fuel_type[10];
    int fuel_tank_cap;
    int seating_cap;
    float city_mileage;
};
int main()
{
    struct car c1;
    return 0;
}
        
                   
                      
// structure with using typedef 
typedef struct {
    char engine[50];
    char fuel_type[10];
    int fuel_tank_cap;
    int seating_cap;
    float city_mileage;
}car;

int main()
{
    car c1;
    return 0;
}
                       
                   

More programs

                   
#include <stdio.h>
#include <string.h>

#define MAX_STUDENTS 500

// Define a structure to store student details
struct Student {
    int roll_number;
    char name[50];
    char department[50];
    char course[50];
    int year_of_joining;
};

int main() {
    // Declare an array of students
    struct Student students[MAX_STUDENTS];
    
    // Get input for each student
    int num_students;
    printf("Enter the number of students: ");
    scanf("%d", &num_students);
    for (int i = 0; i < num_students; i++) {
        printf("Enter details for student %d:\n", i + 1);
        printf("Roll Number: ");
        scanf("%d", &students[i].roll_number);
        printf("Name: ");
        scanf("%s", students[i].name);
        printf("Department: ");
        scanf("%s", students[i].department);
        printf("Course: ");
        scanf("%s", students[i].course);
        printf("Year of Joining: ");
        scanf("%d", &students[i].year_of_joining);
    }
    
    // Print out the details of all the students
    printf("Details of all students:\n");
    for (int i = 0; i < num_students; i++) {
        printf("Roll Number: %d\n", students[i].roll_number);
        printf("Name: %s\n", students[i].name);
        printf("Department: %s\n", students[i].department);
        printf("Course: %s\n", students[i].course);
        printf("Year of Joining: %d\n", students[i].year_of_joining);
    }
    return 0;
}                        
                   
               
                   
#include <stdio.h>

struct Date
{
    int day;
    int month;
    int year;
};

struct Student
{
    int rollNumber;
    char name[50];
    char department[20];
    char course[20];
    struct Date yearOfJoining;
};

int main()
{
    struct Student students[50];
    int i;

    // Input data for all students
    for (i = 0; i < 50; i++)
    {
        printf("Enter Roll Number for Student %d: ", i+1);
        scanf("%d", &students[i].rollNumber);

        printf("Enter Name for Student %d: ", i+1);
        scanf("%s", students[i].name);

        printf("Enter Department for Student %d: ", i+1);
        scanf("%s", students[i].department);

        printf("Enter Course for Student %d: ", i+1);
        scanf("%s", students[i].course);

        printf("Enter Year of Joining for Student %d (dd/mm/yyyy): ", i+1);
        scanf("%d/%d/%d", &students[i].yearOfJoining.day, &students[i].yearOfJoining.month, &students[i].yearOfJoining.year);

        printf("\n");
    }

    // Print all records
    printf("Roll Number\tName\tDepartment\tCourse\tYear of Joining\n");
    for (i = 0; i < 50; i++)
    {
        printf("%d\t\t%s\t%s\t\t%s\t%d/%d/%d\n", students[i].rollNumber, students[i].name, students[i].department, students[i].course, students[i].yearOfJoining.day, students[i].yearOfJoining.month, students[i].yearOfJoining.year);
    }

    return 0;
}                           
                   
               

Reference ↓