- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Arrays of Structures
![]() Share with a Friend |
C Programming - C Arrays of Structures
C Arrays of Structures
In C, an array of structures is a way to store multiple instances of a structure type in a contiguous block of memory. Each element in the array represents an individual structure, allowing you to manage and manipulate collections of related data efficiently.
Syntax for Declaring an Array of Structures
C
struct StructureName {
data_type member1;
data_type member2;
// additional members
};
struct StructureName arrayName[array_size];
Example: Array of Structures
Defining and Using an Array of Structures
C
#include <stdio.h>
// Define a structure
struct Student {
int id;
char name[50];
float marks;
};
int main() {
// Declare an array of structures
struct Student students[3];
// Initialize the array of structures
students[0].id = 101;
students[0].marks = 85.5;
snprintf(students[0].name, sizeof(students[0].name), "Alice");
students[1].id = 102;
students[1].marks = 78.0;
snprintf(students[1].name, sizeof(students[1].name), "Bob");
students[2].id = 103;
students[2].marks = 92.3;
snprintf(students[2].name, sizeof(students[2].name), "Charlie");
// Display the data
for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("ID: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n\n", students[i].marks);
}
return 0;
}
Output:
Student 1:
ID: 101
Name: Alice
Marks: 85.50
Student 2:
ID: 102
Name: Bob
Marks: 78.00
Student 3:
ID: 103
Name: Charlie
Marks: 92.30
Initializing an Array of Structures
You can initialize an array of structures at the time of declaration.
Example:
C
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
// Array of structures with initialization
struct Point points[3] = {
{10, 20},
{30, 40},
{50, 60}
};
// Accessing and printing array elements
for (int i = 0; i < 3; i++) {
printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);
}
return 0;
}
Output:
Point 1: (10, 20)
Point 2: (30, 40)
Point 3: (50, 60)
Passing an Array of Structures to a Function
An array of structures can be passed to a function for processing.
Example:
C
#include <stdio.h>
struct Employee {
int id;
char name[50];
float salary;
};
void displayEmployees(struct Employee employees[], int size) {
for (int i = 0; i < size; i++) {
printf("Employee %d:\n", i + 1);
printf("ID: %d\n", employees[i].id);
printf("Name: %s\n", employees[i].name);
printf("Salary: %.2f\n\n", employees[i].salary);
}
}
int main() {
struct Employee employees[2] = {
{1, "Alice", 50000},
{2, "Bob", 60000}
};
displayEmployees(employees, 2);
return 0;
}
Output:
Employee 1:
ID: 1
Name: Alice
Salary: 50000.00
Employee 2:
ID: 2
Name: Bob
Salary: 60000.00
Use Cases of Arrays of Structures
- Managing Student Records: Store and process data for multiple students.
- Employee Management Systems: Track employee details like ID, salary, and name.
- Inventory Management: Maintain product details in a shop or warehouse.
- Geographical Points: Store coordinates for graphical or mapping applications.
Advantages
- Efficient Data Management: Combines similar types of data in one place.
- Simplifies Code: Access elements using indices, reducing complexity.
- Easy to Pass: Entire arrays can be passed to functions for processing.
Conclusion
An array of structures in C provides a robust way to handle collections of related data. It allows programmers to organize and manipulate large datasets efficiently, improving modularity and maintainability of the code.
