C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Array of Structures
  • Initializing Array of Structures
  • Accessing Array of Structures
  • Looping Through Array of Structures
  • Array Within a Structure
  • Passing Array of Structures to Functions

Was this helpful?

  1. Structure

Structures and Arrays

Combining structures with arrays in C allows you to manage collections of structured data efficiently. Let's explore how to work with structures and arrays together:

Array of Structures

  • You can create an array of structures to represent a collection of related data.

    struct Point {
        int x;
        int y;
    };
    
    struct Point pointsArray[5];

Here, pointsArray is an array of Point structures.

Initializing Array of Structures

  • You can initialize an array of structures during declaration.

    struct Point pointsArray[] = {{1, 2}, {3, 4}, {5, 6}};

This initializes an array of Point structures with specific values.

Accessing Array of Structures

  • Accessing elements in an array of structures involves using indices and the dot (.) operator.

    pointsArray[0].x = 10;
    pointsArray[0].y = 20;

Looping Through Array of Structures

  • Use loops to iterate through an array of structures.

    for (int i = 0; i < 5; ++i) {
        printf("Point %d: (%d, %d)\n", i + 1, pointsArray[i].x, pointsArray[i].y);
    }

Array Within a Structure

  • You can have an array as a member of a structure.

    struct Student {
        char name[50];
        int grades[3]; // Array of grades
    };
    
    struct Student student1;

Passing Array of Structures to Functions

  • You can pass an array of structures to functions for processing.

    void displayPoints(struct Point arr[], int size) {
        for (int i = 0; i < size; ++i) {
            printf("Point %d: (%d, %d)\n", i + 1, arr[i].x, arr[i].y);
        }
    }

Call the function with displayPoints(pointsArray, 5);.

Arrays of structures provide a versatile way to handle complex data sets. They are especially useful when dealing with collections of similar entities, such as a list of points in a 2D space or a group of students with grades.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousStructureNextNested Structures

Was this helpful?