Beruflich Dokumente
Kultur Dokumente
(../index.html)
Page 1 of 11
Advertisements
Array of Structure
Array within Structure
Array of Structure
Structure is collection of different data type. An object of structure represents a single record
in memory, if we want more than one record of structure type, we have to create an array of
structure or object. As we know, an array is a collection of similar type, therefore an array can
be of structure type.
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
Page 2 of 11
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
Page 3 of 11
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
Page 4 of 11
#include<stdio.h>
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void main()
{
int i;
struct Employee Emp[ 3 ];
//Statement 1
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);
}
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
Page 5 of 11
Output :
Enter details of 1 Employee
Enter Employee Id : 101
Enter Employee Name : Suresh
Enter Employee Age : 29
Enter Employee Salary : 45000
Enter details of 2 Employee
Enter Employee Id : 102
Enter Employee Name : Mukesh
Enter Employee Age : 31
Enter Employee Salary : 51000
Enter details of 3 Employee
Enter Employee Id : 103
Enter Employee Name : Ramesh
Enter Employee Age : 28
Enter Employee Salary : 47000
Details of Employees
101
Suresh
102
Mukesh
103
Ramesh
29
31
28
45000
51000
47000
In the above example, we are getting and displaying the data of 3 employee using array of
object. Statement 1 is creating an array of Employee Emp to store the records of 3
employees.
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
Page 6 of 11
// normal variable
// array variable
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
Page 7 of 11
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
struct Student
{
int Roll;
char Name[25];
int Marks[3];
int Total;
float Avg;
};
Page 8 of 11
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);
}
Output :
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
Page 9 of 11
In the above example, we have created an array Marks[ ] inside structure representing 3
marks of a single student. Marks[ ] is now a member of structure student and to access Marks
[ ] we have used dot operator(.) along with object S.
Related topics
- Jump statements - break, continue, goto (12-C-Jump-Statements.aspx)
- C storage classes, Local variable, Global variable, External variable, Register variable. (14C-Storage-Classes.aspx)
- Passing & Returning Structure from Function? (22-C-Structure-Function.aspx)
- Explain bit field in c with example? (25-C-Bit-Field.aspx)
- C Preprocrssors - #if, #else, #elif, #endif (26-C-Preprocrssors.aspx)
Advertisement
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
Page 10 of 11
Follow us
(C-Programming-Introduction.aspx) (C-Programming-Introduction.aspx)
(C-Programming-Introduction.aspx) (C-Programming-Introduction.aspx)
Advertisements
Site Map
Learn C Programming
Learn C++ Programming
Learn Core Java
Learn VB.NET
Learn C Sharp.net
Examples
C Examples
C++ Examples
Core Java Examples
Visual Basic.NET Examples
C Sharp.net Examples
About Us
This website is designed for readers who have less or no programming experience. Even
the experienced programmers will find this website equally useful. We have covered all
the basic of C, C++, C#, JAVA, VB.NET, ASP.NET, etc..., programming language with easy
examples and their descriptions.
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
7/24/2016
http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx
Page 11 of 11
7/24/2016