Example of Array within Structure in C
The below example demonstrates how we can initialize and use the array within structure in C.
// C program to demonstrate the array within structures
#include <string.h>
#include <stdio.h>
// Defining array within structure
struct Employee {
// character array to store name of the employee
char Name[20];
int employeeID;
// integer array to maintain the record of attendanc eof
// the employee
int WeekAttendence[7];
};
int main()
{
// defining structure of type Employee
struct Employee emp;
// adding data
emp.employeeID = 1;
strcpy(emp.Name, "Rohit");
int week;
for (week = 0; week < 7; week++) {
int attendence;
emp.WeekAttendence[week] = week;
}
printf("\n");
// printing the data
printf("Emplyee ID: %d - Employee Name: %s\n",
emp.employeeID, emp.Name);
printf("Attendence\n");
for (week = 0; week < 7; week++) {
printf("%d ", emp.WeekAttendence[week]);
}
printf("\n");
return 0;
}
Output
Emplyee ID: 1 - Employee Name: Rohit Attendence 0 1 2 3 4 5 6
Array within Structure in C
In C, a structure is a user-defined data type that allows us to combine data of different data types. While an array is a collection of elements of the same type. In this article, we will discuss the concept of an array that is declared as a member of the structure.