- C Programming Tutorial
- C - Home
- 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 - Booleans
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- C - Storage Classes
- 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
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- 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
- C - Functions
- C - Main Functions
- 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
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- 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
- C - Strings
- C - Array of Strings
- C - Special Characters
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Nested Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C - Arrays of Structures
In C, 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 struct 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 in the beginning of the code, so that its type can be used inside any of the functions. You can declare an array of structure and later on fill data in it, or you can initialize it at the time of declaration itself.
Initialize struct array
Let use define a struct type named as 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 the memory? Since we have an array of three elements, of struct whose size is 32 bytes, the array occupies 32X3 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 |
Declare 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;
We can also accept data from the user to fill the array
Read struct array
In the following code, a for loop is employed to accept inputs for the title, price and pages elements of each struct element of the array.
Example
#include <stdio.h> #include <string.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 Price: %7.2lf No of Pages: %d\n", b[i].title, b[i].price, b[i].pages); } return 0; }
Output
List of books Title: Learn C Price: 650.50 No of Pages: 325 Title: C Pointers Price: 175.00 No of Pages: 225 Title: C Pearls Price: 250.00 No of Pages: 325
Example
In the example below, a struct type call student is defined. Its elements are name, marks in phy, che and maths; and the percentage. An array of three struct student types is declared and 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> #include <string.h> struct student{ char name[10]; int physics, chem, math; double percent; }; int main (){ struct student s[3]; strcpy(s[0].name, " Ravi "); s[0].physics = 50; s[0].chem = 60; s[0].math =70; strcpy(s[1].name, " Kiran "); s[1].physics = 55; s[1].chem = 66; s[1].math =77; strcpy(s[2].name, " Anil "); s[2].physics = 45; s[2].chem = 55; s[2].math = 65; int i; for (i=0; i<3; i++){ s[i].percent = (double)(s[i].physics + s[i].math + s[i].chem)/3; } printf("\nName\tPhy\tChe\t\Maths\tPercent\n"); for (i=0; i<3; i++) { printf("%s\t%d\t%d\t%d\t%5.2lf\n", s[i].name, s[i].physics, s[i].chem, s[i].math, s[i].percent); } return 0; }
Output
Name Phy Che Maths Percent Ravi 50 60 70 60.00 Kiran 55 66 77 66.00 Anil 45 55 65 55.00
Sort struct array
Let us take another example of struct array. The assay of book struct type is sorted in ascending order of the price by implementing bubble sort technique. Note that elements of one struct variable can be directly assigned to other struct variable directly with assignment operator.
Example
#include <stdio.h> #include <string.h> struct book{ char title[10]; 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 Price: %7.2lf No of Pages: %d\n", b[i].title, b[i].price, b[i].pages); } return 0; }
Output
List of books in ascending order of price Title: C Pointers Price: 175.00 No of Pages: 225 Title: C Pearls Price: 250.00 No of Pages: 250 Title: Learn C Price: 650.50 No of Pages: 325
Pointer to struct array
We can also declare a pointer to struct array. C uses −> operator as the indirection operator to access internal elements of the struct variables.
Example
#include <stdio.h> #include <string.h> struct book{ char title[10]; 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; int i; for (i=0; i<3; i++){ printf("Title: %s Price: %7.2lf No of Pages: %d\n", ptr->title, ptr->price, ptr->pages); ptr++; } return 0; }
Output
Title: Learn C Price: 650.50 No of Pages: 325 Title: C Pointers Price: 175.00 No of Pages: 225 Title: C Pearls Price: 250.00 No of Pages: 250
To Continue Learning Please Login
Login with Google