0% found this document useful (0 votes)
2 views

Array Strcture

The document provides an overview of structures in C programming, including defining structures, using arrays within structures, and implementing nested structures. It also covers passing structures by value and by reference, along with examples of employee and student structures. Additionally, it discusses the differences in size between structures and unions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array Strcture

The document provides an overview of structures in C programming, including defining structures, using arrays within structures, and implementing nested structures. It also covers passing structures by value and by reference, along with examples of employee and student structures. Additionally, it discusses the differences in size between structures and unions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Structure array

struct struct-name
{ datatype var1;
datatype var2;
--------------------
datatype varN;
};
struct struct-name obj [ size ];
#include<stdio.h>
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary
};
void main() {
int i;
struct Employee Emp[ 3 ];
for(i=0;i<3;i++)
{
printf("\nEnter details of %d Employee",i+1);
printf("\n\tEnter Employee Id : ");
scanf("%d",&Emp[i].Id);
printf("\n\tEnter Employee Name : ");
scanf("%s",&Emp[i].Name);
printf("\n\tEnter Employee Age : ");
scanf("%d",&Emp[i].Age);
printf("\n\tEnter Employee Salary : ");
scanf("%ld",&Emp[i].Salary);
}
printf("\nDetails of Employees");
for(i=0;i<3;i++)
printf("\n%d\t%s\t%d\t%ld",Emp[i].Id,Emp[i].Name,Emp[i].Age,Emp[i].Salary);
Array within Structure
struct struct-name
{
datatype var1; // normal variable
datatype array [size]; // array variable
---------
---------
datatype varN;

};

struct struct-name obj;


struct Student
{
int Roll;
char Name[25];
int Marks[3]; //Statement 1 : array of marks
int Total;
float Avg;
};
void main() {
int i;
struct Student S;
printf("\n\nEnter Student Roll : ");
scanf("%d",&S.Roll);
printf("\n\nEnter Student Name : ");
scanf("%s",&S.Name);
S.Total = 0;
for(i=0;i<3;i++)
{
printf("\n\nEnter Marks %d : ",i+1);
scanf("%d",&S.Marks[i]);
S.Total = S.Total + S.Marks[i]; }
S.Avg = S.Total / 3;
printf("\nRoll : %d",S.Roll);
printf("\nName : %s",S.Name);
printf("\nTotal : %d",S.Total);
printf("\nAverage : %f",S.Avg); }
Nested Structure
struct structure1
{
-------
------
-- - - -
};
struct structure2
{
-----
----
------
---
struct structure1 obj;
};
#include<stdio.h>
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};
struct Employee
{
int Id;
char Name[25];
float Salary;
struct Address Add;
};
void main()
{
int i;
struct Employee E;
printf("\n\tEnter Employee Id : ");
scanf("%d",&E.Id);
printf("\n\tEnter Employee Name : ");
scanf("%s",&E.Name);
printf("\n\tEnter Employee Salary : ");
scanf("%f",&E.Salary);

printf("\nDetails of Employees");
printf("\n\tEmployee Id : %d",E.Id);
printf("\n\tEmployee Name : %s",E.Name);
printf("\n\tEmployee Salary : %f",E.Salary);
printf("\n\tEnter Employee House No : ");
scanf("%s",&E.Add.HouseNo);
printf("\n\tEnter Employee City : ");
scanf("%s",&E.Add.City);
printf("\n\tEnter Employee House No : ");
scanf("%s",&E.Add.PinCode);

printf("\n\tEmployee House No : %s",E.Add.HouseNo);


printf("\n\tEmployee City : %s",E.Add.City);
printf("\n\tEmployee House No : %s",E.Add.PinCode);
}
Passing Structure by Value
#include<stdio.h>
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void Display(struct Employee);
void main()
{
struct Employee Emp = {1,"Kumar",29,45000};
Display(Emp);
}

void Display(struct Employee E)


{
printf("\n\nEmployee Id : %d",E.Id);
printf("\nEmployee Name : %s",E.Name);
printf("\nEmployee Age : %d",E.Age);
printf("\nEmployee Salary : %ld",E.Salary);
}
Passing Structure
by Reference
#include<stdio.h>
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void Display(struct Employee*);
void main()
{
struct Employee Emp = {1,"Kumar",29,45000};
Display(&Emp);
}

void Display(struct Employee *E)


{
printf("\n\nEmployee Id : %d",E->Id);
printf("\nEmployee Name : %s",E->Name);
printf("\nEmployee Age : %d",E->Age);
printf("\nEmployee Salary : %ld",E->Salary);
}
#include<stdio.h>
struct Employee1
{
int Id; char Name[25];
long Salary;
};
union Employee2
{
int Id;
char Name[25];
long Salary;
};
void main()
{
printf("\nSize of Employee1 is : %d",sizeof(Employee1));
printf("\nSize of Employee2 is : %d",sizeof(Employee2));
}

You might also like