Sunteți pe pagina 1din 11

Array of Structure in C, Array within Structure in C

(../index.html)

Page 1 of 11

C Array of Structure, C Array within Structure


PROGRAMS (../SHOWPROGRAM.ASPX)

Advertisements

Array and Structure in C


We can use structure and array as :

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

Array of Structure in C, Array within Structure in C

Page 2 of 11

Syntax for declaring structure array


struct struct-name
{
datatype var1;
datatype var2;
------------------datatype varN;
};
struct struct-name obj [ size ];

http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx

7/24/2016

Array of Structure in C, Array within Structure in C

Page 3 of 11

Example for declaring structure array

http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx

7/24/2016

Array of Structure in C, Array within Structure in C

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

Array of Structure in C, Array within Structure in C

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.

Array within Structure


As we know, structure is collection of different data type. Like normal data type, It can also
store an array as well.

http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx

7/24/2016

Array of Structure in C, Array within Structure in C

Page 6 of 11

Syntax for array within structure


struct struct-name
{
datatype var1;
datatype array [size];
------------------datatype varN;
};

// normal variable
// array variable

struct struct-name obj;

http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx

7/24/2016

Array of Structure in C, Array within Structure in C

Page 7 of 11

Example for array within structure

http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx

7/24/2016

Array of Structure in C, Array within Structure in C

struct Student
{
int Roll;
char Name[25];
int Marks[3];
int Total;
float Avg;
};

Page 8 of 11

//Statement 1 : array of marks

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

Array of Structure in C, Array within Structure in C

Page 9 of 11

Enter Student Roll : 10


Enter Student Name : Kumar
Enter Marks 1 : 78
Enter Marks 2 : 89
Enter Marks 3 : 56
Roll : 10
Name : Kumar
Total : 223
Average : 74.00000

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

Array of Structure in C, Array within Structure in C

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.

COPYRIGHT 2084 COMPANY NAME

http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx

7/24/2016

Array of Structure in C, Array within Structure in C

http://www.tutorialdost.com/C-Programming-Tutorial/23-C-Structure-Array.aspx

Page 11 of 11

7/24/2016

S-ar putea să vă placă și