Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Nested structure in C access with variable

Nested structure in C access with variable

Posted on January 20, 2018January 10, 2020

Table of Contents

  • Nested structure in C access with variable
    • Nested structure in C access with normal variable –
    • Nested structure access with pointer variable  –
    • Similar post

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
Structure with function in C Language                    Structure with function C++ Language
 Structure with the pointer in C Language                     Structure with the pointer in C++
Function in C Language                                                 
 
Nested function  in C        
 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes