0% found this document useful (0 votes)
757 views9 pages

Array of Structures in C Explained

An array of structures allows storing information about multiple entities with different data types. It defines a structure with various data members and then declares an array of that structure type. This allows collecting related data together under one name for each entity. The document then provides an example of an array of structures to store details of 5 students like roll number, name, etc. It demonstrates how to declare the structure, define the array, take input and print the stored records.

Uploaded by

gafos82267
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
757 views9 pages

Array of Structures in C Explained

An array of structures allows storing information about multiple entities with different data types. It defines a structure with various data members and then declares an array of that structure type. This allows collecting related data together under one name for each entity. The document then provides an example of an array of structures to store details of 5 students like roll number, name, etc. It demonstrates how to declare the structure, define the array, take input and print the stored records.

Uploaded by

gafos82267
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Array of Structures C
  • Array within a Structure
  • Difference between Array of Structures and Array within Structures

Array of Structures C

An array of structres in C can be defined as the collection of multiple structures variables where each
variable contains information about different entities. The array of structures in C are used to store
information about multiple entities of different data types. The array of structures is also known as the
collection of structures.

An array of structure in C programming is a collection of different datatype


variables, grouped together under a single name.

Array of structures
 The most common use of structure in C programming is an array of structures.
 To declare an array of structure, first the structure must be defined and then an array variable of
that type should be defined.

Array of Structures
An array is a collection of data items of the same type. Each element of the
array can be int, char, float, double, or even a structure. We have seen that a
structure allows elements of different data types to be grouped together
under a single name. This structure can then be thought of as a new data type
in itself. So, an array can comprise elements of this new data type. An array
of structures finds its applications in grouping the records together and
provides for fast access.
Below is a demonstration of an array of structures. The array holds the
details of the students in a class. The details include the roll number,
grade, and marks, which have been grouped under a structure (record).
There exists one record for each student. This is how a collection of related
variables can be assembled under a single entity for enhancing the clarity of
code and increase its efficiency.

Example of an array of structures that stores information


of 5 students and prints it.
#include<stdio.h>
#include <string.h>

struct student
{
int rollno;
char name[10];
};

int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for (i=0;i<5;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<5;i++)
{
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}

}
Array within a Structure
A structure is a data type in C that allows a group of related variables to be
treated as a single unit instead of separate entities. A structure may contain
elements of different data types – int, char, float, double, etc. It may also
contain an array as its member. Such an array is called an array within a
structure. An array within a structure is a member of the structure and can be
accessed just as we access other elements of the structure.
a program that uses the concept of the array within a structure. The
program displays the record of a student comprising the roll number, grade,
and marks secured in various subjects. The marks in various subjects have
been stored under an array called marks. The whole record is stored under a
structure called a candidate.

As we know, the structure is a collection of the different data types. Like


normal data types, It can also store an array as well. In arrays within the
structure, the member of the structure is an array.

The syntax for array within the structure:

Struct struct-name
{
datatype var1; // normal variable
datatype array [size]; // array variable
----------
----------
datatype varN;
};
struct struct-name obj;

Example program for array within structure:

#include<stdio.h>
#include<string.h>
struct Student
{
int Roll;
char Name[10];
int Marks[3]; //array of marks
int Total;
float Avg;
};
int main()
{
int i;
struct Student S;
printf("\n\nEnter Student Roll : ");
scanf("%d",&[Link]);
printf("\n\nEnter Student Name : ");
scanf("%s",&[Link]);
[Link] = 0;
for(i=0;i<3;i++)
{
printf("\n\nEnter Marks %d : ",i+1);
scanf("%d",&[Link][i]);
[Link] = [Link] + [Link][i];
}
[Link] = [Link] / 3;
printf("\nRoll : %d",[Link]);
printf("\nName : %s",[Link]);
printf("\nTotal : %d",[Link]);
printf("\nAverage : %f",[Link]);
}

#include<stdio.h>

struct student {
char name[50];
char Class[100];
int roll_number;
float marks[5];
};

int main() {
struct student s[2];
for (int i = 0; i < 2; i++) {
printf("\nEnter details of student %d\n", i +
1);

printf("Enter name: ");


scanf("%s", s[i].name);

printf("\nEnter roll no: ");


scanf("%d", &s[i].roll_number);

printf("\nEnter class: ");


scanf("%s", s[i].Class);
for (int j = 0; j < 5; j++) {
printf("\nEnter the marks in subject %d (out
of 100): ", j + 1);
scanf("%f", &s[i].marks[j]);
}
printf("\n");
}

printf("\n");
printf("Name\t\tRoll
no\t\t\tClass\t\t\t\tMarks\n");
for (int i = 0; i < 2; i++) {
printf("%s\t\t%d\t\t\t%s\t\t",
s[i].name, s[i].roll_number, s[i].Class);
for (int j = 0; j < 5; j++) {
printf("%.2f\t", s[i].marks[j]);
}
printf("\n");
}

return 0;
}

Output:

Enter details of student 1


Enter name: Aaradhya

Enter roll no: 1

Enter class: A

Enter the marks in subject 1 (out of 100): 100

Enter the marks in subject 2 (out of 100): 99

Enter the marks in subject 3 (out of 100): 98

Enter the marks in subject 4 (out of 100): 97

Enter the marks in subject 5 (out of 100): 99

Enter details of student 2


Enter name: Scaler

Enter roll no: 2

Enter class: A

Enter the marks in subject 1 (out of 100): 100

Enter the marks in subject 2 (out of 100): 100

Enter the marks in subject 3 (out of 100): 99

Enter the marks in subject 4 (out of 100): 98

Enter the marks in subject 5 (out of 100): 100

Name Roll no Class


Marks
Aaradhya 1 A 100.00 99.00 98.0
Scaler 2 A 100.00
100.00 99.00 98.00 100.00

Difference between Array of Structures and Array within


Structures
Below is the tabular difference between the Array within a Structure and Array of
Structures:
Parameter Array within a Structure Array of Structures

A structure contains an array as its member An array in which each element


Basic idea variable is of type structure

Access Can be accessed using the dot operator just as Can be accessed by indexing
Parameter Array within a Structure Array of Structures

we access other elements of the structure just as we access an array

struct class { int a, b, c; }


struct class { int ar[10]; } a1, a2, a3;
Syntax students[10];

Common questions

Powered by AI

The syntax for declaring a structure that includes an array as one of its members is as follows: 'struct struct-name {datatype var1; datatype array[size]; ...};' where 'var1' could be a non-array variable and 'array' a member array to store multiple elements of a similar type, like marks .

To declare and initialize a structure with an array of integers, you define a struct like 'struct Example {int numbers[5];};'. This structure can be utilized to store related numeric data, such as marks or scores, allowing for coherent data management through a singular structured entity. This enables easy iteration over the array and facilitates operations like summing or averaging elements .

A student record system could utilize an array of structures by having an array where each element represents an individual student’s record structure, comprising fields like roll number, name, and a nested array for holding subject marks. An array within a structure allows each student structure to contain a member array that stores marks for different subjects, efficiently organizing subject-specific data together with other individual student data, thus facilitating comprehensive data management .

An array of structures consists of an array where each element is a structure type, allowing for quick access to structured records like student data, by indexing. In contrast, an array within a structure refers to a structure that includes an array as one of its member variables, used to organize related data types like marks secured in multiple subjects under a single entity structure .

For an array of structures, access is performed through indexing, where each structure element can be accessed using an index position in the array. In contrast, an array within a structure is accessed using the dot operator to first navigate to the specific structure instance, followed by standard indexing to access a particular element within the member array .

Arrays within structures encapsulate data by bundling an array as a coherent component of a larger data entity. This practice aids complexity management by logically grouping related data under a single entity, promoting a modular approach to data organization, reducing global namespace sprawl, and clarifying data semantics through structured assignment of attributes like marks under a candidate’s structure .

To calculate total and average marks using an array within a structure, you define a structure (e.g., 'Student') that includes a member array for marks. By iterating over the array within a student's structure, accumulate the marks for a 'Total' and compute 'Average' by dividing 'Total' by the number of subjects. For example, in a program where a student's marks array is looped over, summing each value to calculate 'Total' and then calculating 'Avg' as 'Total / 3' .

Arrays of structures offer computational efficiency by allowing compact storage and easy retrieval of large datasets, like student records. By organizing data in a unified array, they minimize memory overhead and facilitate fast indexed access, enhancing the speed of operations like searches, updates, and sorting on student data, making them ideal for database applications reliant on structured data management .

An array of structures enhances the clarity and efficiency of a C program by grouping similar entities, such as student records, into succinct collections. This reduces redundancy by allowing sets of related variables like roll numbers, names, and grades to be managed within a single array accessed by indexing. Consequently, it simplifies code management and improves performance by providing fast access to structured, related data sets .

Including an array as a member of a structure facilitates the representation of complex data by allowing a consolidated form to store multiple data entries of the same type within a single structure instance. For example, storing the marks of various subjects in an array within the 'candidate' structure collects all marks-related data under one entity, hence simplifying data handling and visibility .

Array of Structures C 
 
An array of structres in C can be defined as the collection of multiple structures variables where
of structures finds its applications in grouping the records together and 
provides for fast access. 
Below is a demonstratio
}     
printf("
Student Information List:");     
for(i=0;i<5;i++) 
{     
  printf("
Rollno:%d, Name:%s",st[i].rollno,st[i
datatype array [size]; // array variable 
- - - - - - - - - - 
- - - - - - - - - - 
datatype varN; 
}; 
struct struct-name ob
{ 
int i; 
struct Student S; 
printf("

Enter Student Roll : "); 
scanf("%d",&S.Roll); 
printf("

Enter Student Name : ")
} 
 
#include<stdio.h> 
 
 
struct student { 
  char name[50]; 
  char Class[100]; 
  int roll_number; 
  float marks[5]; 
};
printf("Name		Roll 
no			Class				Marks
"); 
  for (int i = 0; i < 2; i++) { 
    printf("%s		%d			%s		",
Enter name: Scaler 
 
Enter roll no: 2 
 
Enter class: A 
 
Enter the marks in subject 1 (out of 100): 100 
 
Enter the marks
Parameter   
Array within a Structure 
Array of Structures 
we access other elements of the structure 
just as we access an a

You might also like