Practical-12
Write a program using structure within structure.
Introduction
In this program, we will learn to use a structure within a structure.
Algorithm
- Start
- Declare a structure called outer_struct, and it stores information about a student.
- Declare a structure called inner_struct, and it stores information about a student.
- Intiliaze the student details.
- Print the student details.
- End
Code
Practical-12.c
#include <stdio.h>
void main()
{
/* STRUCTURE within STRUCTURE */
/* first define the outer structure */
struct outer_struct {
char name[20];
int age;
/* now define the inner structure */
struct inner_struct {
int marks;
char grade;
} inner;
} outer;
/* assign values to outer structure */
strcpy(outer.name, "Naruto");
outer.age = 20;
/* assign values to the inner structure */
outer.inner.marks = 80;
outer.inner.grade = 'A';
/* display the structure member values */
printf("name: %s\n", outer.name);
printf("age: %d\n", outer.age);
printf("marks: %d\n", outer.inner.marks);
printf("grade: %c\n", outer.inner.grade);
}
Output
name: Naruto
age: 20
marks: 80
grade: A
Explanation
In this program, we have used a structure within a structure. We have defined a structure called outer_struct, and it stores information about a student. We have defined another structure called inner_struct, and it stores information about a student. We have initialized the student details. We have printed the student details.