C Language

Nested structure in C access with variable

Nested structure in C access with variable

In this tutorial, we will discuss the nested structure in C access with the variable. What is the structure and nesting structure? I will discuss that in another post. We can declare structure inside another structure that is called the nested structure. We can declare structure members in every structure as possible.

Structure variables can be accessed in two way.

  1. Structure in C access with normal variable
  2. Structure access  with the pointer variable

Nested structure in C access with normal variable

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.

Program 1

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

using normal structure variable in this program.

#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

Nested structure access with pointer variable

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.

Program 1
#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

Similar post

Structure in C Language                                       Structure in C++ Language
Function in C Language                                             
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.