Posted By:
23 Jun, 2013 1:08 pm PDT

Structures in C

How can use arrays in structures?
Attachment:None


This program shows how to use arrays in structures. An example taken here to illustrate that is to enter student details.

A structure named student is formed which contains members such as

  • roll number,
  • computer marks,
  • maths marks.

In the main function we declare the structure as an array. So we can enter details of n students as the user requires.

 

#include"stdio.h"
#include"conio.h"
#include"process.h"

struct student
{
int rollno;
int cmarks;
int mmarks;
};

int main()
{
struct student std[10];
int n,i,t,j;
printf("\n How many students : ");
scanf("%d",&n);
if(n>10)
{
 printf("\n You have entered wrong");
 getch(); 
 exit(1);
}
for(i=0;i<n;i++)
 {
 printf("\n Enter Record of Student.\n");
 printf(" Enter the Rollno of Student : ");
 scanf("%d",&std[i].rollno);
 printf(" Enter the Computer marks of Student : ");
 scanf("%d",&std[i].cmarks);
 printf(" Enter the Mathematics marks of Student : ");
 scanf("%d",&std[i].mmarks);
}

printf("\n The detail of Student(s) is as.\n");
printf(" ROLLNO   COMPUTER   MATHEMATICS\n");
printf("*********************************\n");
for(i=0;i<n;i++)
 {
printf("   %d         %d         %d         \n",std[i].rollno,std[i].cmarks,std[i].mmarks);
}
getch();
}

 

Please see the video for more details

Was it useful?  



struct Book

{

     char bname[20];

     int pages;

     char author[20];

     float price;

}b1[3] = {

           {"Let us C",700,"YPK",300.00},

           {"Wings of Fire",500,"APJ Abdul Kalam",350.00},

           {"Complete C",1200,"Herbt Schildt",450.00}

         };

 After declaration we intialize the the structure  with pre defined values. as defined , we initialze accordingly , enclosed in curl braces. 

and as usual the structure definition ends with a semicolon . 

 


Was it useful?  




Please login to reply to this problem.