TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Friday 15 May 2020

Nested Structure In C | Nested Structure In C In Hindi | Nested Structure In C Programming Example

Nested Structure in C


C provides us the feature of nesting one structure within another structure by using which, complex data types are created.

image of nested structure in c
Fig: Nested structure in c

Program

#include<stdio.h>  

struct address   

{  

    char city[20];  

    int pin;  

    char phone[14];  

};  

struct employee  

{  

    char name[20];  

    struct address add;  

};  

void main ()  

{  

    struct employee emp;  

 

    printf("Enter employee information?\n");  

 

    scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);  

 

    printf("Printing the employee information....\n");  

 

    printf("name: %s\nCity: %s\nPincode: %d\nPhone: %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);  

 

}  

 

Output

Enter employee information?

Pramod       

Delhi          

201301 

9540225110

Printing the employee information.... 

name: Pramod

City: Delhi 

Pincode: 201301

Phone: 9450225110

 

The structure can be nested in the following ways.

1.    By separate structure

2.    By Embedded structure

1) Separate structure

We create two structures, but the dependent structure should be used inside the main structure as a member.

 

Example

     struct Date  

{       

      int dd;  

      int mm;  

       int yyyy;    

         };  

     struct Employee  

{            

       int id;  

      char name[20];  

      struct Date doj;  

      }emp1;  

 

2) Embedded structure


The embedded structure enables us to declare the structure inside the structure. Hence, it requires less line of codes but it can not be used in multiple data structures.


Example:

     struct Employee  

{     

    int id;  

    char name[20];  

     struct Date  

    {  

      int dd;  

      int mm;  

      int yyyy;   

    }doj;  

}   emp1;  


Accessing Nested Structure


We can access the member of the nested structure by Outer_Structure.

    e1.doj.mm  

     e1.doj.yyyy  

 

Program 

     

    #include <stdio.h>  

    #include <string.h>  

     struct Employee  

{     

     int id;  

   char name[20];  

   struct Date  

    {  

      int dd;  

      int mm;  

      int yyyy;   

    }doj;  

    }e1;    

    int main( )  

                   

      {            

     e1.id=101;  

     strcpy(e1.name, "Pramod Dwivedi");

    e1.doj.dd=08;  

    e1.doj.mm=07;  

    e1.doj.yyyy=2000;  

  

    printf( "employee id : %d\n", e1.id);  

    printf( "employee name : %s\n", e1.name);  

            printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.d d   ,e1.doj.mm,e1.doj.yyyy);  

     return 0;  

            }             


Output:


employee id : 101

employee name : Pramod Dwivedi

employee date of joining (dd/mm/yyyy) : 08/07/2000

 


No comments:

Post a Comment