Array of Structures in C
0 193
In C programming, an array of structures allows you to store multiple instances of a structured data type in contiguous memory locations.
This organizational approach is beneficial when dealing with related data sets, as it provides a unified container for handling similar entities efficiently.
Arrays of structures combine the benefits of arrays and structures, enabling the storage and manipulation of complex data sets.
Each element of the array represents a distinct structure, and you can access individual members of each structure using the dot operator (.).
By defining a structure that encapsulates related data fields, you create a blueprint for each element in the array.
This blueprint ensures uniformity across all elements, simplifying data management and enhancing code readability.
Syntax Array of Structure in C
struct structure_name array_name[array_size];
struct: Keyword indicating the declaration of a structure.
structure_name: Name of the structure type defined previously using the struct keyword.
array_name: Name of the array variable.
array_size: Number of elements in the array.
Example:
#include<stdio.h>Output:#include<string.h> // Define a structure to represent a book struct Book { char title[100]; char author[100]; int pages; float price; }; int main() { // Declare an array of Book structures struct Book library[3]; // Initialize the first book strcpy(library[0].title, "The Catcher in the Rye"); strcpy(library[0].author, "J.D. Salinger"); library[0].pages = 224; library[0].price = 10.99; // Initialize the second book strcpy(library[1].title, "To Kill a Mockingbird"); strcpy(library[1].author, "Harper Lee"); library[1].pages = 336; library[1].price = 12.50; // Initialize the third book strcpy(library[2].title, "1984"); strcpy(library[2].author, "George Orwell"); library[2].pages = 328; library[2].price = 9.99; // Display information about each book for (int i = 0; i < 4; i++) { printf("Book %d\n", i + 1); printf("Title: %s\n", library[i].title); printf("Author: %s\n", library[i].author); printf("Pages: %d\n", library[i].pages); printf("Price: $%.2f\n", library[i].price); printf("\n"); } return 0; }
Book 1 Title: The Catcher in the Rye Author: J.D. Salinger Pages: 224 Price: $10.99 Book 2 Title: To Kill a Mockingbird Author: Harper Lee Pages: 336 Price: $12.50 Book 3 Title: 1984 Author: George Orwell Pages: 328 Price: $9.99
In this example, we define a structured Book to represent information about a book, including its title, author, number of pages, and price.
We then declare an array of Book structures named library, with a size of 3.
We initialize each element of the library array with information about different books.
Finally, we iterate through the array and display the information about each book.
Uses of Array of Structure in C
An array of structures in C has various uses across different applications. Some common use cases include:
Database Management: Storing records of multiple entities, such as employees, students, or customers, where each structure represents a single record with different attributes.
Data Representation: Organizing r elated data into structured formats for easier management and processing. For example, in graphical applications, each structure may represent a point in 2D or 3D space.
Data Analysis: Handling data with multiple attributes, such as coordinates in a 3D space or financial transactions with various details. Array of structures allows for efficient storage and manipulation of such complex data sets.
Information Storage: Efficiently managing and accessing data in memory for applications requiring structured data storage. This can include applications dealing with large datasets, where organizing data into structures simplifies access and manipulation.
Record Keeping: Maintaining records of objects with multiple properties. For instance, in a library management system, each structure may represent a book with attributes like title, author, ISBN, etc.
Share:
Comments
Waiting for your comments