In this tutorial you’ll be learning about struct types or structure and an example of Nested Structure in C Programming. In C programming, a struct keyword is used to indicate structures that resembles a collection of variables with different types but under a single name.

Defining Structures

Unlike any other variables for which we immediately start with datatype, in case of structures we need to define its datatype. To define a structure in c programming, the struct keyword is used.

Syntax of struct

struct structureName {
    dataType fieldMember1;
    dataType fieldMember2;
    ....
};

For example,

struct Student {
    char name[50];
    int redgNo;
    float grade;
};

In this example, we defined a struct Student type, and now we can begin creating variables of this type.

Creating struct variables

Declaring the struct type, there is no memory or storage allocated right away in the system. To allocate memory of a give structure type and to store values for that type, we need to create variables.

Example of creating structure variables:

struct Student {
    // code
};

int main() {
    struct Student student1, student2, classOf2022[30];
    return 0;
}

A different approach of creating struct variables can be like:

struct Student {
    // code
} student1, student2, classOf2022[30];

In both of above approaches:

  • student1 and student2 are struct Student variables
  • classOf2022[] is a struct Student array of size 30.

Access members of a Structure

There are two types of operators used for accessing members of a structure.

  1. . – Member operator
  2. -> – Structure pointer operator

Suppose, you want to access the grade of student2. Here’s how you can do it.

student2.grade

Example Of Nested Structure in C Programming

With this basic idea of Structure in C, we’ll be solving this complex example of Nested Structure in C Programming.

Problem: Write a C Program to create Nested Structure for the table shown below. Store data of 45 students in the structure. Also create a menu driven structure on console with following menu, and perform respective task as mentioned.

Example Student Data with Nested Structure in C Programming
Example Student Data with Nested Structure in C Programming

Create Structure for given table representation

  1. Print Name and Address of students who have Malepatan as street name.
  2. Calculate average GPA of Student and Print Name and GPAs of students who scores average GPA of 3.2 or above.
  3. Print Name and Date of birth of students who were born before 2000.

Video Tutorial

Understanding Structures in C Programming

I have used Dev-C++ to write the program. You can choose any compilers for C from this list of Top 30 Best IDEs/Compilers of C to write this program.

C Program to create Nested Structure and perform operations with menu driven structure

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

#define LEN 50
#define SIZE 2

struct DOB {
	int day;
	char month[10];
	int year;
};

struct Student{
	char name[LEN+1];
	int roll_no;
	float gpa[8];

	struct {
		int ward_no;
		char street_name[LEN+1];
		char city[LEN+1];
	}address;

	struct DOB dob;
};

int main(){
	int i, j, choice=0;
	struct Student class2018[SIZE];
	float avg_gpa;

	for(i=0;i<=SIZE-1;i++){
		printf("\nEnter name of student: ");
		scanf("%s", class2018[i].name);

		printf("\nEnter roll no: ");
		scanf("%d", &class2018[i].roll_no);

		for(j=0;j<8;j++){
			printf("\nEnter GPA of %s for semester %d", class2018[i].name, j+1);
			scanf("%f", &class2018[i].gpa[j]);
		}

		printf("\nEnter Address Information");
		printf("\nEnter ward no: ");
		scanf("%d", &class2018[i].address.ward_no);

		printf("\nEnter street name: ");
		scanf("%s", class2018[i].address.street_name);

		printf("\nEnter city: ");
		scanf("%s", class2018[i].address.city);

		printf("\nDate of Birth Information");
		printf("\nEnter Day (10) : ");
		scanf("%d", &class2018[i].dob.day);

		printf("\nEnter month (jan) : ");
		scanf("%s", class2018[i].dob.month);

		printf("\nEnter year (1999) : ");
		scanf("%d", &class2018[i].dob.year);
	}

	while( choice != 4){
		printf("\n\nMenu\n\n");
		printf("\n1.	Print Name and Address of students who have Malepatan as street name.");
		printf("\n2.	Print Name and GPAs of students who scores average GPA of 3.2 or above.");
		printf("\n3.	Print Name and Date of birth of students who were born before 2000.");
		printf("\n4.    Exit");

		printf("Enter your choice (1 - 4) : ");
		scanf("%d", &choice);

		switch(choice){
			case 1:
				printf("\nStudent with address Malepatan are: \n");
				for(i=0;i<=SIZE-1;i++){

					if(strcmp(class2018[i].address.street_name, "Malepatan") == 0){
						printf("%s\t%s\n", class2018[i].name, class2018[i].address.street_name);
					}
				}
			break;

			case 2:
				printf("\nStudent with average GPA > 3.2\n");
				for(i=0;i<SIZE;i++){
					avg_gpa = 0;
					for(j=0;j<8;j++){
						avg_gpa += class2018[i].gpa[j];
					}
					avg_gpa = avg_gpa/8.0;
					if(avg_gpa > 3.2){
						printf("%s\t%f\n", class2018[i].name, avg_gpa);
					}
				}
			break;

			case 3:
				printf("\nStudent who were born before 2000\n");
				for(i=0;i<SIZE;i++){
					if(class2018[i].dob.year < 2000){
						printf("%s\t%d-%s-%d\n", class2018[i].name, class2018[i].dob.day,
							class2018[i].dob.month, class2018[i].dob.year);
					}
				}
			break;

			case 4:
				exit(0);
			break;

			default:
				printf("Invalid Choice!!");
		}
	}
	return 0;
}

If you like this tutorial, don’t forget to check all other tutorials and examples in C Programming and the whole class on C Programming in Github.