Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C Structs and Pointers: A Practical Guide

Mastering C Structs and Pointers: A Practical Guide

Discover how to harness pointers to navigate struct members, and learn dynamic memory allocation for flexible C data structures.

Before diving into pointers with structs, familiarize yourself with these foundational topics:


Using Pointers to Reference Structs

Below is a concise example that shows how to declare a struct and a pointer that references it.

struct Person {
    int member1;
    int member2;
    /* additional members */
};

int main(void) {
    struct Person *ptr, harry;
    /* ptr can now point to any Person instance */
}

In this snippet, ptr is a pointer to a struct Person instance.


Example: Accessing Struct Members Through a Pointer

To read or write struct fields via a pointer, use the -> operator.

#include <stdio.h>

struct Person {
    int age;
    float weight;
};

int main(void) {
    struct Person *personPtr, person1;
    personPtr = &person1;   /* point to person1 */

    printf("Enter age: ");
    scanf("%d", &personPtr->age);

    printf("Enter weight: ");
    scanf("%f", &personPtr->weight);

    printf("\nDisplaying:\n");
    printf("Age: %d\n", personPtr->age);
    printf("Weight: %f", personPtr->weight);
    return 0;
}

Here, personPtr = &person1; stores the address of person1 in personPtr. Subsequent access via personPtr->field is equivalent to (*personPtr).field, offering concise syntax.


Dynamic Memory Allocation for Structs

When the number of struct instances required isn’t known at compile time, allocate memory at runtime using the C standard library.

Example: Allocating an Array of Structs

#include <stdio.h>
#include <stdlib.h>

struct Person {
    int age;
    float weight;
    char name[30];
};

int main(void) {
    struct Person *ptr;
    int i, n;

    printf("Enter the number of persons: ");
    scanf("%d", &n);

    ptr = (struct Person*)malloc(n * sizeof(struct Person));
    if (!ptr) {
        fprintf(stderr, "Memory allocation failed\n");
        return EXIT_FAILURE;
    }

    for (i = 0; i < n; ++i) {
        printf("Enter first name and age respectively: ");
        scanf("%s %d", (ptr + i)->name, &(ptr + i)->age);
    }

    printf("\nDisplaying Information:\n");
    for (i = 0; i < n; ++i) {
        printf("Name: %s\tAge: %d\n", (ptr + i)->name, (ptr + i)->age);
    }

    free(ptr);
    return 0;
}

Running the program yields:

Enter the number of persons:  2
Enter first name and age respectively:  Harry 24
Enter first name and age respectively:  Gary 32

Displaying Information:
Name: Harry	Age: 24
Name: Gary	Age: 32

This example demonstrates how to create n instances of struct Person on the heap, read user input into each, and then display the results.



C Language

  1. Mastering C# While and Do‑While Loops: Syntax, Examples, and Best Practices
  2. Understanding C# Structs: Definition, Usage, and Key Differences
  3. C++ Pointers and Arrays: Mastering the Relationship
  4. Mastering While and Do‑While Loops in C: Practical Examples
  5. Mastering C Pointers: A Practical, Expert‑Guided Tutorial
  6. Understanding the Relationship Between Arrays and Pointers in C
  7. C Programming: Passing Addresses & Pointers to Functions – A Practical Guide
  8. Mastering C Structs: Definition, Variables, Access & Advanced Examples
  9. Mastering C Structs: Passing, Returning, and Reference Handling
  10. C++ Structs Explained with a Practical Example