We can also use a Array as member variable inside the structure is called Array in Structure.
Syntax:
struct structure_name
{
data_type array_variable1;
data_type array_variable2;
---------------------------------
data_type array_variableN;
};
Example :
struct student
{
char name[20];
int roll;
float marks[5];
};
struct student s;
Here, name[20] contains the 20 elements, store the name of the student with length 20 (Max). marks[5] contains the 5 elements, stores the marks of five subjects of a students in marks[0],marks[1],…marks[4].
These elements can be accessed with its appropriate subscripts/indexes.
Example Array with in Structure
#include<stdio.h>
#include<conio.h> //Structure is Created with Array struct abc { char name[20]; int rollno; float marks[5]; }; // Initialization a Structure void main() { struct abc s1; int i; clrscr(); printf("Enter the name\n"); scanf("%s",s1.name); printf("\nEnter the roll no\n"); scanf("%d",&s1.rollno); printf("\nEnter the Marks\n"); for(i=0;i<5;i++) { scanf("%f",&s1.marks[i]); } // Accessing structure member printf("the name is-%s",s1.name); printf("\nthe roll no is-%d",s1.rollno); printf("\nthe Marks is-\n"); for(i=0;i<5;i++) { printf("\n%f",s1.marks[i]); } getch(); } |
Watch Video Here...