0
6.7kviews
Explain Nested Structure. Write a program using nested structure to create an Array of structure to store the details of N students.

The details are,

a. Student name.

b. Student Roll no

c. Marks of Physics, chemistry, maths.

Calculate the total of P-C-M. display the data in the format.

Subject : Structured Programming Approach

Title : Arrays, String, Structures and Union

Difficulty : Medium

1 Answer
1
184views

Nested structure: Nested structure in C is nothing but structure within structure.One structure can be declared inside other structure as we declare structure members inside a structure. The structure variables can be a normal structure variable or a pointer variable to access the data.Nested structure in c language can have another structure as a member.There are two ways to define nested structure in c language:

1) Separate structure: We can create 2 structures, but dependent structure should be used inside the main structure as a member. Let's see the code of nested structure.

2) Embedded structure: We can define structure within the structure also. It requires less code than previous way. But it can't be used in many structures.

The syntax of nested structure is given as:

struct structure_name

{

    data_type variable_name;

    _ _ _ _ _ _ _

    struct

    {

        data_type variable_name;

        _ _ _ _ _ _ _

        internal_structure_name;

        _ _ _ _ _ _ _ _

    }
}

Program:

#include<stdio.h>

#include<conio.h>

struct students

{

    char name[30];

    int roll_no, total;

    struct

    {

    int physics, chemistry, maths;

    }

    marks;

};

void main()

{

    struct students n[100];

    int n, i, j;

    clrscr();

    printf(“Enternumber of students : ”);

    scanf(“%d”, &n);

    for (i=0 ; i<=n-1 ; i++)

    {

        printf(“Enterfollowing detailsofstudent:-”);

        printf(“Name : ”);

        scanf(“%s”, & n[i].name);

        printf(“Roll number : ”);

        scanf(“%d”, & n[i].roll_no);

        printf(“Marks in Physics : ”);

        scanf(“%d”, & n[i].marks.physics);

        printf(“Marks inChemistry : ”);

        scanf(“%d”, & n[i].marks.chemistry);

        printf(“Marks in Mathematics : ”);

        scanf(“%d”, & n[i].marks.maths);

        n[i].total=n[i].marks.physics + n[i].marks.chemistry + n[i].marks.maths;

    }

    printf(“\n Name \t Roll Number \t Total \n”);

    printf(“ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n”);

    for (i=0 ; i<=n-1 ; i++)

    {

        printf(“%s \t %d \t %d \n”, n[i].name, n[i].roll_no,n[i].total);

    }

    getch();

}

Output:

Enter number of students : 3

Enter following details of student :-

Name : Saurav Palande

Roll number : 45

Marks in Physics: 50

Marks in Chemistry : 60

Marks in Mathematics : 30

Enter following details of student :-

Name : Sayali Rajapurkar

Roll number : 40

Marks in Physics : 50

Marks in Chemistry : 60

Marks in Mathematics : 40

Enter following details of student :-

Name : Amrit Shah

Roll number : 65

Marks in Physics : 60

Marks in Chemistry : 60

Marks in Mathematics : 60

Name Roll Number Total
Saurav Palande 45 140
Sayali Rajapurkar 40 150
Amrit Shah 65 180
Please log in to add an answer.