Wednesday

Structures within Structures (Nested Structures) :


Structures within Structures (Nested Structures) :

Structures can be used as structures within structures. It is also called as 'nesting of structures'.
Syntax:

struct structure_nm
{
 <data-type> element 1;
 <data-type> element 2;
 - - - - - - - - - - -
 - - - - - - - - - - -
 <data-type> element n;
 
 struct structure_nm
 {
  <data-type> element 1;
  <data-type> element 2;
  - - - - - - - - - - -
  - - - - - - - - - - - 
  <data-type> element n;
 }inner_struct_var;
}outer_struct_var;


Example :

struct stud_Res
{
 int rno;
 char nm[50];
 char std[10];
 
 struct stud_subj
 {
  char subjnm[30];
  int marks;
 }subj;
}result;
In above example, the structure stud_Res consists of stud_subj which itself is a structure with two members. Structure stud_Res is called as 'outer structure' while stud_subj is called as 'inner structure.' The members which are inside the inner structure can be accessed as follow :
result.subj.subjnm
result.subj.marks

Program :


/*  Program to demonstrate nested structures.

Creation Date : 23 Nov 2010 04:04:01 AM

Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>

struct stud_Res
{
 int rno;
 char std[10];
 struct stud_Marks
 {
  char subj_nm[30];
  int subj_mark;
 }marks;
}result;

void main()
{
 clrscr();
 printf("\n\t Enter Roll Number : ");
 scanf("%d",&result.rno);
 printf("\n\t Enter Standard : ");
 scanf("%s",result.std);
 printf("\n\t Enter Subject Code : ");
 scanf("%s",result.marks.subj_nm);
 printf("\n\t Enter Marks : ");
 scanf("%d",&result.marks.subj_mark);
 printf("\n\n\t Roll Number : %d",result.rno);
 printf("\n\n\t Standard : %s",result.std);
 printf("\nSubject Code : %s",result.marks.subj_nm);
 printf("\n\n\t Marks : %d",result.marks.subj_mark);
 getch();
}

Output :


 Enter Roll Number : 1
 
 Enter Standard : MCA(Sci)-I
 
 Enter Subject Code : SUB001
 
 Enter Marks : 63
 
 
 Roll Number : 1
 
 Standard : MCA(Sci)-I
Subject Code : SUB001

 Marks : 63_


Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |