Nested structure in C
Table of Contents
Structure variables can be accessed in two way.
We can access of the structure using the normal variable in C language
The structure variable can be normal structure variable to access the data in C programming language.
This program explains how to use structure inside another structure using the normal variable in the C Programming language.
D_O_Birth structure is declared inside stu_details structure
#include <stdio.h>
#include <stdlib.h>
struct D_O_Birth
{
int day;
int month;
int year;
};
struct stu_detials
{
int stu_id;
char name[20];
float payment;
struct D_O_Birth date;
};
int main()
{
struct stu_detials s;
printf("Enter the name:\n");
scanf("%s",&s.name);
printf("Enter the student id:\n");
scanf("%d",&s.stu_id);
printf("Enter the payment:\n");
scanf("%f",&s.payment);
printf("Enter the date:\n");
scanf("%d",&s.date.day);
printf("Enter the month:\n");
scanf("%d",&s.date.month);
printf("Enter the year:\n");
scanf("%d",&s.date.year);
printf("Display details:\n\n");
printf("Student id is:%d\n",s.stu_id);
printf("Student name is:%d\n",s.name);
printf("Student payment is:%f\n",s.payment);
printf("Student DOB is: %d- %d-%d\n",s.date.day,s.date.month, s.date.year );
getch();
return 0;
}
When the above code
is executed, The following output is displayed
We can access of the structure using pointer variable in C language
The structure variable can be pointer variable to access the data in C programming language.
This program explains how to use structure inside another structure(Nested structure) using the pointer variable in the C Programming language.
An address structure is declared inside Employee structure using the pointer as a structure variable in this progra
m.
#include <stdio.h>
#include <stdlib.h>
struct Address{//structure Address
char DoorNo[20];//member 1
char AreaNo[20];//member 2
long postalcode[100];//member 3
};
struct Employee{
char Name[25]; // member 1
char Gender[10]; // member 2
int Age; // member 3
double salary; // member 4
struct Address adr;//implement the address in employee
};
int main()
{
struct Employee emp;
struct Address adr;
struct Employee *e;//pointer for Employee
struct Address *a;//pointer for address
e=&emp;//assign address of emp to pointer e
a=&adr;//assign address of adr to pointer a
printf("Enter the employee details: \n");
printf("Name: ");
scanf("%s",&e->Name);
printf("Gender: ");
scanf("%s",&e->Gender);
printf("Age: ");
scanf("%d",&e->Age);
printf("salary: ");
scanf("%lf",&e->salary);
//here input employee details
printf("\nEnter the address details: \n");
printf("Door No: ");
scanf("%s",&a->DoorNo);
printf("Area No: ");
scanf("%s",&a->AreaNo);
printf("Postal code: ");
scanf("%1i",&a->postalcode);
//here input Address details
printf("\nEmployee details: \n");
printf("Name : %s\n",emp.Name);
printf("Gender : %s\n",emp.Gender);
printf("Age : %d\n",emp.Age);
printf("Salary : %lf\n\n",emp.salary);
//Display employee details
printf("DoorNo: %s \n",adr.DoorNo);
printf("AresNo : %s\n",adr.AreaNo);
printf("Postal code : %li\n",adr.postalcode);
//Display Addrress details
getch();
return 0;
}
When the above code is executed the following output is displayed
Program2
This program explains how to use structure inside another structure using a pointer variable in the C Programming language.
school_Details structure is declared inside stu_Details structure using the pointer structure variable in this program
#include <stdio.h>
#include <stdlib.h>
struct school_Details{
char sch_Name[100];
int sch_Code;
};
struct stu_Details
{
int stu_Id;
char stu_Name[50];
float avg;
struct school_Details sch;
}stu_Data,*stu_Data1;
int main()
{
struct stu_Details stu_Data={1,"Kumar",56.7,"Hartley college",1008};
stu_Data1=&stu_Data;
printf("Student id is: %d \n",stu_Data1->stu_Id);
printf("Average is: %.2f \n",stu_Data1->avg);
printf("School code is: %d \n",stu_Data1->sch.sch_Code);
printf("School name is: %s \n",stu_Data1->sch.sch_Name);;
getch();
return 0;
}
When the above code is executed, The following output is displayed
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.