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
  • Pointer to Structure
  • Dynamic Memory Allocation for Structures
  • Accessing Members via Pointers
  • Passing Structures to Functions via Pointers
  • Array of Pointers to Structures
  • Deallocating Memory

Was this helpful?

  1. Structure

Structures and Pointers

Combining structures with pointers in C allows for dynamic memory allocation and manipulation of structured data. Let's explore how structures and pointers can be used together:

Pointer to Structure

  • You can declare a pointer to a structure in a similar way to declaring a pointer to other data types.

    struct Point {
        int x;
        int y;
    };
    
    struct Point *pointPtr;

Dynamic Memory Allocation for Structures

  • Allocate memory for a structure dynamically using the malloc function.

    struct Point *dynamicPoint = (struct Point *)malloc(sizeof(struct Point));

Ensure to check if the memory allocation was successful before proceeding.

Accessing Members via Pointers

  • Use the arrow (->) operator to access members of a structure through a pointer.

    dynamicPoint->x = 10;
    dynamicPoint->y = 20;

Passing Structures to Functions via Pointers

  • Pass structures to functions using pointers for more efficient memory handling.

    void initializePoint(struct Point *ptr, int xVal, int yVal) {
        ptr->x = xVal;
        ptr->y = yVal;
    }

Call the function: initializePoint(dynamicPoint, 30, 40);.

Array of Pointers to Structures

  • Create an array of pointers to structures for managing multiple structured entities.

    struct Point *pointsArray[5];

Deallocating Memory

  • Properly deallocate dynamically allocated memory using the free function.

    free(dynamicPoint);

Always free memory when it's no longer needed to prevent memory leaks.

Structures and pointers together enable dynamic handling of structured data, offering flexibility and efficiency in memory management. Whether allocating memory for structures dynamically or passing structures to functions via pointers, this combination is crucial for working with complex data structures.

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

PreviousNested StructuresNextStructures and Functions

Was this helpful?