- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Array of Structures in C
In C programming, the struct keyword is used to define a derived data type. Once defined, you can declare an array of struct variables, just like an array of int, float or char types is declared. An array of structures has a number of use-cases such as in storing records similar to a database table where you have each row with different data types.
Usually, a struct type is defined at the beginning of the code so that its type can be used inside any of the functions. You can declare an array of structures and later on fill data in it or you can initialize it at the time of declaration itself.
Initializing a Struct Array
Let us define a struct type called book as follows −
struct book{ char title[10]; double price; int pages; };
During the program, you can declare an array and initialize it by giving the values of each element inside curly brackets. Each element in the struct array is a struct value itself. Hence, we have the nested curly brackets as shown below −
struct book b[3] = { {"Learn C", 650.50, 325}, {"C Pointers", 175, 225}, {"C Pearls", 250, 250} };
How does the compiler allocate memory for this array? Since we have an array of three elements, of struct whose size is 32 bytes, the array occupies "32 x 3" bytes. Each block of 32 bytes will accommodate a "title", "price" and "pages" element.
L | E | A | R | N | C | 675.50 | 325 | ||||
C | P | O | I | N | T | E | R | S | 175 | 225 | |
C | P | E | A | R | L | S | 250 | 250 |
Declaring a Struct Array
You can also declare an empty struct array. Afterwards, you can either read the data in it with scanf() statements or assign value to each element as shown below −
struct book b[3]; strcpy(b[0].title, "Learn C"); b[0].price = 650.50; b[0].pages=325; strcpy(b[1].title, "C Pointers"); b[1].price = 175; b[1].pages=225; strcpy(b[2].title, "C Pearls"); b[2].price = 250;250 b[2].pages=325;
Reading a Struct Array
We can also accept data from the user to fill the array.
Example 1
In the following code, a for loop is used to accept inputs for the "title", "price" and "pages" elements of each struct element of the array.
#include <stdio.h> struct book{ char title[10]; double price; int pages; }; int main(){ struct book b[3]; strcpy(b[0].title, "Learn C"); b[0].price = 650.50; b[0].pages = 325; strcpy(b[1].title, "C Pointers"); b[1].price = 175; b[1].pages = 225; strcpy(b[2].title, "C Pearls"); b[2].price = 250; b[2].pages = 325; printf("\nList of Books:\n"); for (int i = 0; i < 3; i++){ printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", b[i].title, b[i].price, b[i].pages); } return 0; }
Output
When you run this code, it will produce the following output −
List of Books: Title: Learn C Price: 650.50 Pages: 325 Title: C Pointers Price: 175.00 Pages: 225 Title: C Pearls Price: 250.00 Pages: 325
Example 2
In this example, a struct type called student is defined. Its elements are "name"; marks in physics, chemistry and maths; and the "percentage".
An array of three struct student types is declared and the first four elements are populated by user input, with a for loop. Inside the loop itself, the "percent" element of each subscript is computed.
Finally, an array of students with their names, marks and percentage is printed to show the marklist.
#include <stdio.h> struct student{ char name[10]; int physics, chemistry, maths; double percent; }; int main(){ struct student s[3]; strcpy(s[0].name, "Ravi"); s[0].physics = 50; s[0].chemistry = 60; s[0].maths = 70; strcpy(s[1].name, "Kiran"); s[1].physics = 55; s[1].chemistry = 66; s[1].maths = 77; strcpy(s[2].name, "Anil"); s[2].physics = 45; s[2].chemistry = 55; s[2].maths = 65; for (int i = 0; i < 3; i++){ s[i].percent = (double)(s[i].physics + s[i].maths + s[i].chemistry)/3; } printf("\nName\tPhysics\tChemistry\t\Maths\tPercent\n"); for(int i = 0; i < 3; i++){ printf("%s \t\t%d \t\t%d \t\t%d \t\t%5.2lf\n", s[i].name, s[i].physics, s[i].chemistry, s[i].maths, s[i].percent); } return 0; }
Output
When you run this code, it will produce the following output −
Name Physics Chemistry Maths Percent Ravi 50 60 70 60.00 Kiran 55 66 77 66.00 Anil 45 55 65 55.00
Sorting a Struct Array
Let us take another example of struct array. Here, we will have the array of "book" struct type sorted in ascending order of the price by implementing bubble sort technique.
Note: The elements of one struct variable can be directly assigned to another struct variable by using the assignment operator.
Example
Take a look at the example −
#include <stdio.h> struct book{ char title[15]; double price; int pages; }; int main(){ struct book b[3] = { {"Learn C", 650.50, 325}, {"C Pointers", 175, 225}, {"C Pearls", 250, 250} }; int i, j; struct book temp; for(i = 0; i < 2; i++){ for(j = i; j < 3; j++){ if (b[i].price > b[j].price){ temp = b[i]; b[i] = b[j]; b[j] = temp; } } } printf("\nList of Books in Ascending Order of Price:\n"); for (i = 0; i < 3; i++){ printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", b[i].title, b[i].price, b[i].pages); } return 0; }
Output
When you run this code, it will produce the following output −
List of Books in Ascending Order of Price: Title: C Pointers Price: 175.00 Pages: 225 Title: C Pearls Price: 250.00 Pages: 250 Title: Learn C Price: 650.50 Pages: 325
Declaring a Pointer to a Struct Array
We can also declare a pointer to a struct array. C uses the indirection operator (→) to access the internal elements of struct variables.
Example
The following example shows how you can declare a pointer to a struct array −
#include <stdio.h> struct book { char title[15]; double price; int pages; }; int main(){ struct book b[3] = { {"Learn C", 650.50, 325}, {"C Pointers", 175, 225}, {"C Pearls", 250, 250} }; struct book *ptr = b; for(int i = 0; i < 3; i++){ printf("Title: %s \tPrice: %7.2lf \tPages: %d\n", ptr -> title, ptr -> price, ptr -> pages); ptr++; } return 0; }
Output
When you run this code, it will produce the following output −
Title: Learn C Price: 650.50 Pages: 325 Title: C Pointers Price: 175.00 Pages: 225 Title: C Pearls Price: 250.00 Pages: 250