Initializing Structure

Normal variable-ஐ போலவே ஒரு structure variable-க்கும் values-ஐ initialize செய்யமுடியும்.

Syntax

struct structure_name{
structure members
};

struct structure_name variable1={value1,value2,...};
struct structure_name variable2={value1,value2,...};
struct structure_name variable3={value1,value2,...};
.......
......

Example

#include<stdio.h>
#include<conio.h>
struct employee{
int no;
char name[20];
};
struct employee e1={12,"srishti"};
struct employee e2={13,"prisci"};
struct employee e3={14,"Yamuna"};

int main(){
	printf("\n %d %s", e1.no, e1.name);
	printf("\n %d %s", e2.no, e2.name);
	printf("\n %d %s", e3.no, e3.name);
return 0;
}

Output:

12  srishti
13  prisci
14  Yamuna

Nested Structure

ஒரு structure variable-ஐ மற்றொரு structure-க்குள், structure member-ஆக declare செய்வது nested structure. ஆகையால் அந்த structure member-ஐ access செய்ய வேண்டுமெனில் இரண்டு structure-ன் variable ஒரு சேர பயன்படுத்தினால்தான் access செய்ய முடியும்.

example

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

struct dob {
int date;
int month;
int year; 
};

struct stud{
int sno;
char sname[10];
struct dob sdob; // structure variable for above structure
char gender;  
};

int main()
{
	struct stud s; //variable for stud
	printf("enter the sno\n");
	scanf("%d",&s.sno);
	printf("enter the student name\n");
	scanf("%s",s.sname);
	printf("enter the dob\n");
	scanf("%d%d%d",&s.sdob.date, &s.sdob.month,&s.sdob.year);
	printf("enter the gender\n");
	scanf("%c",&s.gender);
	printf("\nYour Record is:");
	printf("\n %d\t %s\t %d %d %d \t %c",  s.sno, s.sname, s.sdob.date, s.sdob.month, s.sdob.year, s.gender);
return 0;
}

Output:

enter the sno
1324
enter the student name
venkat
enter the dob
17 10 2018
M
Your Record is:
1324 venkat 17 10 2018 M

Comments