Offers
Thanks for visiting www.technoexam.com. Stay connected with us for more latest programming updates...

You are here : Home >> Tutorials >> C

Go
Basic C Tutorial

Contents
What is C ?
History
Structure of C Program
Variables & Keywords
Constants
Data Types
Operators
Operators Precedence
Conditional Statements
if Statement
if else Statement
nested if else
switch case
Looping Statements
While loop
Do While loop
For loop
Break
Continue
Goto
Functions
Passing Value
Returning Value
Passing & Returning
Recursion
Storage Class
Array
Structure
Pointer
Union
Strings
Header File
Programs Library
Projects Library
Learn More+

Contact Us

+91-8806583119

Structures within Structures (Nested Structures) in C

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
#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;

int main(void)
{
	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();
	return 0;
}

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_

Download PDF
Published on: 12th Nov 2010, Last Updated on: 18th Dec 2014

Mr. Dinesh Kudache
AUTHOR: Mr. Dinesh Kudache

Mr. Dinesh is the Founder and CEO of technoexam.com. He had 6+ years experience in Application Programming, System Programming, Web Design and Development, Google Expertise. He had written 5000+ various articles on programming. Browse more articles by this author.

Link this post on your Blog/Website :