Array of Structures in C
In C language allows the use of arrays as structure members. we used arrays of characters inside a structure. Similarly, we are able to use single or multi-dimensional arrays of type int or float. For example, the structure declaration is valid;
Syntax:
struct student
{ int id;
char name[50];
int mark1,mark2;
}s[3];
Here, the member subject contains 3 elements, s[0], s[1] and s[2]. These elements can be accessed using appropriate subscripts.
Examples:
#include<stdio.h>
#include <string.h>
struct student
{ int id;
char name[50];
int mark1,mark2;
}s[3];
void main( )
{
s[1].id=101;
strcpy(s[1].name, "Arunkumar");
s[1].mark1=89;
s[1].mark2=58;
s[2].id=105;
strcpy(s[2].name, "Manivannan");
s[2].mark1=69;
s[2].mark2=88;
s[3].id=102;
strcpy(s[3].name, "Deepalakshmi");
s[3].mark1=79;
s[3].mark2=98;
for(int i=1;i<4;i++){
printf( "Student %d id : %d\n",i, s[i].id);
printf( "Student %d name : %s\n",i, s[i].name);
printf( "Student %d mark1 : %d\n",i, s[i].mark1);
printf( "Student %d mark2 : %d\n",i, s[i].mark2);
}
}
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com