google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Array with in Structure -Structure in C

Array with in Structure -Structure in C

0


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...

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top