Array of Structure
- Till now we have seen array of basic data types like array of int ,array of float ,array character etc.
- But now we will see how to make array of structure variable.
- So,let's define a structure
Let's define a structure Student with name ,rollno, div as it's attribute.
Definition:
struct student //structure name
{
int rollno; //member1 ,integer variable which will store,
//student roll no.
char name[20]; //member2 a character array of 20 character
//which will store name of student.
char div; //member3 a character variable,
//which will store division of student.
};
struct student //structure name { int rollno; //member1 ,integer variable which will store, //student roll no. char name[20]; //member2 a character array of 20 character //which will store name of student. char div; //member3 a character variable, //which will store division of student. };
Syntax:Declaration of structure array
struct structure_name structure_variable_name[size_of_array];
struct structure_name structure_variable_name[size_of_array];
Declaration of structure student array
void main()
{
//declaration of student array.
struct student s[10];
}
void main() { //declaration of student array. struct student s[10]; }
Initialization of Structure array:
void main()
{
//declaration of student array.
struct student s[3] = {{1,"Roy",'A'}, //data of first student
{2,"Jason",'B'},//data of second student
{3,"Tom",'A'}};//data of third student
}
void main() { //declaration of student array. struct student s[3] = {{1,"Roy",'A'}, //data of first student {2,"Jason",'B'},//data of second student {3,"Tom",'A'}};//data of third student }
How to Access members of structure array.
- As,you know every array element has an unique index number,Which starts from 0 index.
- Means first array element wil have index 0,second array element will have index 1 and So on...
- Let's see memory view of structure array,by looking it you will get an great understanding of array structure
Syntax | Accessing member of structure array
struct_variable_name[index_number].member_name;
struct_variable_name[index_number].member_name;
Let's access the rollno of each student.
Accessing:Rollno of first student
int roll_1;
roll_1 = s[0].rollno;
int roll_1; roll_1 = s[0].rollno;
Accessing:Rollno of second student
int roll_2;
roll_1 = s[1].rollno;
int roll_2; roll_1 = s[1].rollno;
Accessing:Rollno of Third student
This how we access members, now we will access names
Accessing : Names
int i;
for(i=0; i<3; i++)
{
printf("\nName of student%d is %s", i, s[i],name);
}
int i; for(i=0; i<3; i++) { printf("\nName of student%d is %s", i, s[i],name); }
Output:
Name of student1 is Roy
Name of student2 is Jason
Name of student3 is Tom
Name of student2 is Jason
Name of student3 is Tom
now we will access div
Accessing : Div
int i;
for(i=0; i<3; i++)
{
printf("\nstudent%d studies in division %c", i, s[i],div);
}
int i; for(i=0; i<3; i++) { printf("\nstudent%d studies in division %c", i, s[i],div); }
Output:
student1 studies in division A
student2 studies in division B
student3 studies in division A
student2 studies in division B
student3 studies in division A
- This is how we access the member of structure array.
- Now we will take structure array as input from user.
we will see program of taking members of structure as Input.
Taking Input:
void main()
{
int i;
//declaration of student array.
struct student s[3];
for(i=0; i<3 ;i++)
{
printf("\nEnter name of student: ");
gets(s[i].name);//taking name of student
printf("\nEnter the student roll no:");
scanf("%d",&s[i].rollno);//taking student rollno
printf("\nstudent division:");
scanf("%c",s[i].div);//taking student division
}
}
void main() { int i; //declaration of student array. struct student s[3]; for(i=0; i<3 ;i++) { printf("\nEnter name of student: "); gets(s[i].name);//taking name of student printf("\nEnter the student roll no:"); scanf("%d",&s[i].rollno);//taking student rollno printf("\nstudent division:"); scanf("%c",s[i].div);//taking student division } }
Output:
Enter name of student:Roy
Enter the student roll no:1
student division:A
Enter name of student:Jason
Enter the student roll no:2
student division:B
Enter name of student:Tom
Enter the student roll no:3
student division:A
Enter the student roll no:1
student division:A
Enter name of student:Jason
Enter the student roll no:2
student division:B
Enter name of student:Tom
Enter the student roll no:3
student division:A
Printing the student data:
int i;
for(i=0; i<3 ;i++)
{
printf("\nstudent roll no and Name is %d and %s and studies in %c division",s[i].rollno,s[i].name,s[i].div);
}
int i; for(i=0; i<3 ;i++) { printf("\nstudent roll no and Name is %d and %s and studies in %c division",s[i].rollno,s[i].name,s[i].div); }
Output:
student roll no and Name is 1 and Roy and studies in A division
student roll no and Name is 2 and Jason and studies in B division
student roll no and Name is 3 and Tom and studies in A division
student roll no and Name is 2 and Jason and studies in B division
student roll no and Name is 3 and Tom and studies in A division
- This how we can take members of structres as input from User.
- This how we can work with array of structure In C.
Further Concept:
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- What are structures?
- Why structures?
- Need of Structure
- Concept of structure
- How to initialize an structure variable?
- How to Access Structure member?
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment