I was wondering what the difference between the two.
Source: https://www.geeksforgeeks.org/array-of-structures-vs-array-within-a-structure-in-c-and-cpp/
Sample Array of Structure
#include <stdio.h>
struct class {
int roll_no;
char grade;
float marks;
};
void display(struct class class_record[3])
{
int i, len = 3;
for (i = 0; i < len; i ) {
printf("Roll number : %d\n",
class_record[i].roll_no);
printf("Grade : %c\n",
class_record[i].grade);
printf("Average marks : %.2f\n",
class_record[i].marks);
printf("\n");
}
}
int main()
{
struct class class_record[3]
= { { 1, 'A', 89.5f },
{ 2, 'C', 67.5f },
{ 3, 'B', 70.5f } };
display(class_record);
return 0;
}
CodePudding user response:
There is no difference between a class type and a structure type in C . They are the same thing.
The code you are showing is C and not valid C . In C class
is a keyword and can not be used to name a type.
You create an array of a class type in C exactly in the same way as you create an array of any other type:
class C { }; // or identically struct instead of class
C c[123];
Here c
is an array of 123 C
objects.