Arrays of Structures in C Programming

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Passing & Returning Structures with Functions in C Programming

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:03 Array of Structures
  • 0:42 Using Typedef With Structures
  • 1:13 Disadvantages of…
  • 2:16 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Login or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Meghalee Goswami
This lesson introduces the ability to use arrays of structures in C programming. It explains how to use typedef statements to declare structures. It also discusses the disadvantages of using parallel arrays as compared to arrays of structures.

Array of Structures

In programming, structure is a composite datatype with a collection of variables. These variables can have different data types and collectively form a structure of a composite datatype. An array of structures is a sequential collection of structures. With structures, you can store mixed record types, and with an array supporting this, you can have a list of mixed record types. The following example shows a structure called student that takes the roll number and name of a student as an input, then stores each record in an array st, which stores three elements. Each element will hold a mixed record.

Here is an example of an array:


#include<stdio.h>
#include <string.h>
struct student{
 int rollno;
 char name[10];
};
int main(){
 int i;
 struct student st[3];
 printf("Enter Records of 3 students");
 for(i=0;i<3;i++){
  printf("\nEnter Rollno:");   scanf("%d",&st[i].rollno);   printf("\nEnter Name:");   scanf("%s",&st[i].name);
 }
 printf("\nStudent Information List:");
 for(i=0;i<3;i++){   printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
 }
return 0;
}



Enter Records of 3 students
Enter Rollno:1
Enter Name:Jenny
Enter Rollno:2
Enter Name:Angela
Enter Rollno:3
Enter Name:Rory
Student Information List:
Rollno:1, Name: Jenny
Rollno:2, Name: Angela
Rollno:3, Name: Rory

Using Typedef With Structures

The typedef command is used to rename structures. This is done so that we can refer to structure variables directly. The previous example has been modified to use a typedef command. The structure has been given a name called Stud. This variable can be used to directly initialize the data members of the structure. We define an object of Stud called record, as you can see appearing in the code below.


#include <stdio.h>
#include <string.h>
typedef struct Student { 
  int rollno;
  char name[10];
} Stud;
int main( ) { 
  Stud record;
  strcpy( record.name, "Nezah Ali");
  record.rollno = 12;
  printf( "Student Name : %s\n", record.name);
  printf( "Roll number: %d\n", record.rollno);
  return 0;
}


Lastly, here's an example of the output:


Student Name : Nezah Ali
Roll number: 12

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support