C Structure is a collection of different data types which are grouped together and each element in a C structure is called member.

Difference between C variable, C array and C structure:

C Structure:

Syntax

struct student
{
int a;
char b[10];
}

Example

a = 10;
b = “Hello”;

C Variable:

int

Syntax: int a;
Example: a = 20;

char

Syntax: char b;
Example: b=’Z’;

C Array:

int

Syntax: int a[3];
Example:
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = ‘\0’;

char

Syntax: char b[10];
Example:
b=”Hello”;

Below table explains following concepts in C structure.

  1. How to declare a C structure?
  2. How to initialize a C structure?
  3. How to access the members of a C structure?

Using normal variable

Using pointer variable

Syntax:
struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};

Syntax:
struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};

Example:
struct student
{
int  mark;
char name[10];
float average;
};

Example:
struct student
{
int  mark;
char name[10];
float average;
};

Declaring structure using normal variable:
struct student report;

Declaring structure using pointer variable:
struct student *report, rep;

Initializing structure using normal variable:
struct student report = {100, “Mani”, 99.5};

Initializing structure using pointer variable:
struct student rep = {100, “Mani”, 99.5};
report = &rep;

Accessing structure members using normal variable:
report.mark;
report.name;
report.average;

Accessing structure members using pointer variable:
report  -> mark;
report -> name;
report -> average;

Example program for C structure:

This program is used to store and access “id, name and percentage” for one student. We can also store and access these data for many students using array of structures. You can check “C – Array of Structures” to know how to store and access these data for many students.

#include <stdio.h>

#include <string.h>

 struct student

{

           int id;

           char name[20];

           float percentage;

};

 

int main()

{

           struct student record = {0}; //Initializing to null

            record.id=1;

           strcpy(record.name, "Raju");

           record.percentage = 86.5;

            printf(" Id is: %d \n", record.id);

           printf(" Name is: %s \n", record.name);

           printf(" Percentage is: %f \n", record.percentage);

           return 0;

}

Example program – Another way of declaring C structure:

In this program, structure variable “record” is declared while declaring structure itself. In above structure example program, structure variable “struct student record” is declared inside main function which is after declaring structure.

C

#include <stdio.h>

#include <string.h>

struct student

{

            int id;

            char name[20];

            float percentage;

} record;

int main()

{

           record.id=1;

            strcpy(record.name, "Raju");

            record.percentage = 86.5;

            printf(" Id is: %d \n", record.id);

            printf(" Name is: %s \n", record.name);

            printf(" Percentage is: %f \n", record.percentage);

            return 0;

}

#include <stdio.h>

#include <string.h>

 struct student

{

            int id;

            char name[20];

            float percentage;

} record;

 int main()

{

             record.id=1;

            strcpy(record.name, "Raju");

            record.percentage = 86.5;

             printf(" Id is: %d \n", record.id);

            printf(" Name is: %s \n", record.name);

            printf(" Percentage is: %f \n", record.percentage);

            return 0;

}

C structure declaration in separate header file:

In above structure programs, C structure is declared in main source file. Instead of declaring C structure in main source file, we can have this structure declaration in another file called “header file” and we can include that header file in main source file as shown below.

Header file name – structure.h

Before compiling and executing below C program, create a file named “structure.h” and declare the below structure.

struct student

{

int id;

char name[20];

float percentage;

} record;

Main file name – structure.c:

In this program, above created header file is included in “structure.c” source file as #include “Structure.h”. So, the structure declared in “structure.h” file can be used in “structure.c” source file.

C

// File name - structure.c

#include <stdio.h>

#include <string.h>

#include "structure.h"   /* header file where C structure is

                            declared */

int main()

{

   record.id=1;

   strcpy(record.name, "Raju");

   record.percentage = 86.5;

   printf(" Id is: %d \n", record.id);

   printf(" Name is: %s \n", record.name);

   printf(" Percentage is: %f \n", record.percentage);

   return 0;

}

// File name - structure.c

#include <stdio.h>

#include <string.h>

#include "structure.h"   /* header file where C structure is

                            declared */

 

int main()

{

 

   record.id=1;

   strcpy(record.name, "Raju");

   record.percentage = 86.5;

 

   printf(" Id is: %d \n", record.id);

   printf(" Name is: %s \n", record.name);

   printf(" Percentage is: %f \n", record.percentage);

   return 0;

}

Output:

Id is: 1

Name is: Raju

Percentage is: 86.500000

USES OF STRUCTURES IN C:

    C Structures can be used to store huge data. Structures act as a database.

    C Structures can be used to send data to the printer.

    C Structures can interact with keyboard and mouse to store the data.

    C Structures can be used in drawing and floppy formatting.

    C Structures can be used to clear output screen contents.

    C Structures can be used to check computer’s memory size etc.

C structure can be accessed in 2 ways in a C program. They are,

    Using normal structure variable

    Using pointer variable

Dot(.) operator is used to access the data using normal structure variable and arrow (->) is used to access the data using pointer variable. You have learnt how to access structure data using normal variable in C – Structure topic. So, we are showing here how to access structure data using pointer variable in below C program.

Example program for C structure using pointer:

In this program, “record1” is normal structure variable and “ptr” is pointer structure variable. As you know, Dot(.) operator is used to access the data using normal structure variable and arrow(->) is used to access data using pointer variable.

C

#include <stdio.h>

#include <string.h>

struct student

{

     int id;

     char name[30];

     float percentage;

};

int main()

{

     int i;

     struct student record1 = {1, "Raju", 90.5};

     struct student *ptr;

     ptr = &record1;    

         printf("Records of STUDENT1: \n");

         printf("  Id is: %d \n", ptr->id);

         printf("  Name is: %s \n", ptr->name);

         printf("  Percentage is: %f \n\n", ptr->percentage);

     return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

        

#include <stdio.h>

#include <string.h>

 

struct student

{

     int id;

     char name[30];

     float percentage;

};

 

int main()

{

     int i;

     struct student record1 = {1, "Raju", 90.5};

     struct student *ptr;

 

     ptr = &record1;    

 

         printf("Records of STUDENT1: \n");

         printf("  Id is: %d \n", ptr->id);

         printf("  Name is: %s \n", ptr->name);

         printf("  Percentage is: %f \n\n", ptr->percentage);

 

     return 0;

}

Output:

Records of STUDENT1:

Id is: 1

Name is: Raju

Percentage is: 90.500000

Example program to copy a structure in C:

There are many methods to copy one structure to another structure in C.

    We can copy using direct assignment of one structure to another structure or

    we can use C inbuilt function “memcpy()” or

    we can copy by individual structure members.

C

#include <stdio.h>

#include <string.h>

struct student

{

    int id;

    char name[30];

    float percentage;

};

int main()

{

    int i;

    struct student record1 = {1, "Raju", 90.5};

    struct student record2, *record3, *ptr1, record4;

    printf("Records of STUDENT1 - record1 structure \n");

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",

            record1.id, record1.name, record1.percentage);

    // 1st method to copy whole structure to another structure

    record2=record1;    

    printf("\nRecords of STUDENT1 - Direct copy from " \

           "record1 \n");

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",

            record2.id, record2.name, record2.percentage);

    // 2nd method to copy using memcpy function

    ptr1 = &record1;

    memcpy(record3, ptr1, sizeof(record1));

    printf("\nRecords of STUDENT1 - copied from record1 " \

           "using memcpy \n");

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",

           record3->id, record3->name, record3->percentage);

    // 3rd method to copy by individual members

    printf("\nRecords of STUDENT1 - Copied individual " \

           "members from record1 \n");

    record4.id=record1.id;

    strcpy(record4.name, record1.name);

    record4.percentage = record1.percentage;

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",

            record4.id, record4.name, record4.percentage);

     return 0;

}

----------------------------------------------------------------------------

#include <stdio.h>

#include <string.h>

 

struct student

{

    int id;

    char name[30];

    float percentage;

};

 

int main()

{

    int i;

    struct student record1 = {1, "Raju", 90.5};

    struct student record2, *record3, *ptr1, record4;

 

    printf("Records of STUDENT1 - record1 structure \n");

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",

            record1.id, record1.name, record1.percentage);

 

    // 1st method to copy whole structure to another structure

    record2=record1;    

 

    printf("\nRecords of STUDENT1 - Direct copy from " \

           "record1 \n");

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",

            record2.id, record2.name, record2.percentage);

 

    // 2nd method to copy using memcpy function

    ptr1 = &record1;

    memcpy(record3, ptr1, sizeof(record1));

 

    printf("\nRecords of STUDENT1 - copied from record1 " \

           "using memcpy \n");

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",

           record3->id, record3->name, record3->percentage);

 

    // 3rd method to copy by individual members

    printf("\nRecords of STUDENT1 - Copied individual " \

           "members from record1 \n");

    record4.id=record1.id;

    strcpy(record4.name, record1.name);

    record4.percentage = record1.percentage;

 

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",

            record4.id, record4.name, record4.percentage);

 

     return 0;

}

NESTED STRUCTURES

include <stdio.h>

#include <string.h>

 

struct student_college_detail

{

    int college_id;

    char college_name[50];

};

 

struct student_detail

{

    int id;

    char name[20];

    float percentage;

    // structure within structure

    struct student_college_detail clg_data;

}stu_data;

 

int main()

{

    struct student_detail stu_data = {1, "Raju", 90.5, 71145,

                                       "Anna University"};

    printf(" Id is: %d \n", stu_data.id);

    printf(" Name is: %s \n", stu_data.name);

    printf(" Percentage is: %f \n\n", stu_data.percentage);

 

    printf(" College Id is: %d \n",

                    stu_data.clg_data.college_id);

    printf(" College Name is: %s \n",

                    stu_data.clg_data.college_name);

    return 0;

}

#include <stdio.h>

#include <string.h>

 

struct student_college_detail

{

    int college_id;

    char college_name[50];

};

 

struct student_detail

{

    int id;

    char name[20];

    float percentage;

    // structure within structure

    struct student_college_detail clg_data;

}stu_data, *stu_data_ptr;

 

int main()

{

  struct student_detail stu_data = {1, "Raju", 90.5, 71145,

                                    "Anna University"};

    stu_data_ptr = &stu_data;

 

    printf(" Id is: %d \n", stu_data_ptr->id);

    printf(" Name is: %s \n", stu_data_ptr->name);

    printf(" Percentage is: %f \n\n",

                         stu_data_ptr->percentage);

 

    printf(" College Id is: %d \n",

                         stu_data_ptr->clg_data.college_id);

    printf(" College Name is: %s \n",

                      stu_data_ptr->clg_data.college_name);

 

    return 0;

}

Passing structure to a Function

========================

#include <stdio.h>

#include <string.h>

 

struct student

{

            int id;

            char name[20];

            float percentage;

};

 

void func(struct student record);

 

int main()

{

            struct student record;

 

            record.id=1;

            strcpy(record.name, "Raju");

            record.percentage = 86.5;

 

            func(record);

            return 0;

}

 

void func(struct student record)

{

            printf(" Id is: %d \n", record.id);

            printf(" Name is: %s \n", record.name);

            printf(" Percentage is: %f \n", record.percentage);

}

Example: Store Information and Display it Using Structure

#include <stdio.h>

struct student

{

    char name[50];

    int roll;

    float marks;

} s;

int main()

{

    printf("Enter information:\n");

    printf("Enter name: ");

    scanf("%s", s.name);

    printf("Enter roll number: ");

    scanf("%d", &s.roll);

    printf("Enter marks: ");

    scanf("%f", &s.marks);

    printf("Displaying Information:\n");

    printf("Name: ");

    puts(s.name);

    printf("Roll number: %d\n",s.roll);

    printf("Marks: %.1f\n", s.marks);

    return 0;

}

DMA

Example: Demonstrate the Dynamic Memory Allocation for Structure

#include <stdio.h>

#include<stdlib.h>

struct course

{

   int marks;

   char subject[30];

};

int main()

{

   struct course *ptr;

   int i, noOfRecords;

   printf("Enter number of records: ");

   scanf("%d", &noOfRecords);

   // Allocates the memory for noOfRecords structures with pointer ptr pointing to the base address.

   ptr = (struct course*) malloc (noOfRecords * sizeof(struct course));

   for(i = 0; i < noOfRecords; ++i)

   {

       printf("Enter name of the subject and marks respectively:\n");

       scanf("%s %d", &(ptr+i)->subject, &(ptr+i)->marks);

   }

   printf("Displaying Information:\n");

   for(i = 0; i < noOfRecords ; ++i)

       printf("%s\t%d\n", (ptr+i)->subject, (ptr+i)->marks);

   return 0;

}