Structure with Pointers

Master Structure with Pointers in C 2025

Structure with Pointers : This beginner-friendly article explains the powerful combination of structures and pointers in C programming. You’ll learn how to define and use pointers to structures, access structure members with the arrow operator (->), dynamically allocate memory for structures using malloc, and pass structures by reference to functions. With easy-to-understand code examples, common interview questions, and practical tips, this guide is perfect for anyone preparing for C programming interviews or trying to strengthen their understanding of pointers and structs. Whether you’re a student or an embedded developer, this article builds a solid foundation in handling structures efficiently using pointers.

Structure with Pointers

Basics Recap: Structure with Pointers Definition and Access

struct Student {
    int id;
    char name[20];
};

Without Structure with Pointers:

struct Student s1 = {101, "Alice"};
printf("%d", s1.id);

With Structure with Pointers:

struct Student s1 = {101, "Alice"};
struct Student *ptr = &s1;
printf("%d", ptr->id);         // Or: (*ptr).id

Accessing Members: Dot (.) vs Arrow (->)

ExpressionMeaning
ptr->idPreferred: Dereference and access member
(*ptr).idAlso valid, but less readable

Using Pointers to Modify Structure with Pointers

void update(struct Student *s) {
    s->id = 202;
    strcpy(s->name, "Bob");
}

int main() {
    struct Student s1 = {101, "Alice"};
    update(&s1);
    printf("%d %s", s1.id, s1.name);
}

✅ This modifies the original structure by passing pointer, not a copy.

Dynamic Memory Allocation for Structure with Pointers

#include <stdlib.h>
struct Student *s = (struct Student *)malloc(sizeof(struct Student));
s->id = 1;
strcpy(s->name, "Charlie");
free(s);

Why use dynamic allocation?

  • When the number of objects isn’t known at compile time
  • Saves stack memory
  • Useful in data structures (linked list, tree)

Array of Structures with Pointers

struct Student students[3] = {
    {101, "Alice"},
    {102, "Bob"},
    {103, "Eve"}
};

struct Student *ptr = students;

for (int i = 0; i < 3; i++) {
    printf("ID: %d, Name: %s\n", (ptr + i)->id, (ptr + i)->name);
}

ptr + i gives address of each element; use -> to access fields.

Self-Referencing Structure (Linked List Use Case)

struct Node {
    int data;
    struct Node *next;
};

✅ Used to build:

  • Linked lists
  • Trees
  • Graphs
struct Node *head = (struct Node *)malloc(sizeof(struct Node));
head->data = 10;
head->next = NULL;

Pointer to Array of Structures (Advanced Usage)

struct Student *arr = malloc(3 * sizeof(struct Student));
arr[0].id = 1;
strcpy(arr[0].name, "A");

✅ Use arr[i] or (*(arr + i)).id

Key Mistakes to Avoid Structure with Pointers

MistakeFix
Using structure pointer without initializationAlways assign memory first
Forgetting -> when using pointerUse ptr->member, not ptr.member
Not freeing dynamically allocated structureAlways use free() after malloc()
Passing structure by value when large in sizeUse pointer to avoid stack overhead
Accessing freed pointerSet to NULL after freeing

Summary Table

ConceptExamplePurpose
Pointer to structurestruct *ptr = &obj;Efficient access/modification
Dynamic allocationmalloc(sizeof(struct))Runtime memory usage
Arrow operator ->ptr->fieldShortcut for pointer access
Self-referencing structurestruct Node *next;Linked list, tree, stack, queue
Array of struct pointersstruct *arr[n];Complex data structures
Memory-mapped I/O with struct(struct *)0xADDREmbedded register mapping

1. Basic Structure Without Pointer

#include <stdio.h>

struct Student {
    int id;
    char name[20];
    float marks;
};

int main() {
    struct Student s1 = {101, "John", 87.5};

    printf("ID: %d\n", s1.id);
    printf("Name: %s\n", s1.name);
    printf("Marks: %.2f\n", s1.marks);

    return 0;
}

2. Pointer to Structure: The Basics

To access members using a pointer to a structure, we use the -> (arrow) operator.

#include <stdio.h>

struct Student {
    int id;
    float marks;
};

int main() {
    struct Student s1 = {102, 91.5};
    struct Student *ptr = &s1;

    printf("ID: %d\n", ptr->id);       // Equivalent to (*ptr).id
    printf("Marks: %.2f\n", ptr->marks);

    return 0;
}

🔍 Interview Tip: Explain both ptr->id and (*ptr).id. They are functionally the same.

3. Dynamically Allocating Structure Using malloc()

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

struct Student {
    int id;
    float marks;
};

int main() {
    struct Student *ptr = (struct Student *)malloc(sizeof(struct Student));

    ptr->id = 103;
    ptr->marks = 75.5;

    printf("ID: %d\n", ptr->id);
    printf("Marks: %.2f\n", ptr->marks);

    free(ptr); // Always free memory
    return 0;
}

🔍 Interview Tip: You may be asked to dynamically create an array of structures.

4. Passing Structure Pointer to Function

#include <stdio.h>

struct Student {
    int id;
    float marks;
};

void display(struct Student *ptr) {
    printf("ID: %d\n", ptr->id);
    printf("Marks: %.2f\n", ptr->marks);
}

int main() {
    struct Student s = {104, 89.0};
    display(&s);  // Pass address
    return 0;
}

🔍 Interview Tip: This is memory-efficient compared to passing the full structure.

5. Array of Structures with Pointers

#include <stdio.h>

struct Student {
    int id;
    float marks;
};

int main() {
    struct Student students[2] = {
        {201, 76.5},
        {202, 88.0}
    };

    struct Student *ptr = students; // Point to first element

    for (int i = 0; i < 2; i++) {
        printf("ID: %d, Marks: %.2f\n", ptr->id, ptr->marks);
        ptr++;
    }

    return 0;
}

6. Nested Structures with Pointers

#include <stdio.h>

struct Date {
    int day, month, year;
};

struct Student {
    int id;
    struct Date dob;
};

int main() {
    struct Student s = {301, {10, 6, 2000}};
    struct Student *ptr = &s;

    printf("DOB: %02d-%02d-%d\n", ptr->dob.day, ptr->dob.month, ptr->dob.year);
    return 0;
}

Interview Questions on Structure with Pointers

  1. What is the difference between ptr->x and (*ptr).x?
    • Both are the same. ptr->x is syntactic sugar for (*ptr).x.
  2. Why use structure pointers instead of structures in functions?
    • Efficient: avoids copying large structure data.
  3. Can we allocate memory dynamically for structure?
    • Yes, using malloc.
  4. How to create a linked list using structure pointer?
    • Each node has a structure with a pointer to the next node.
  5. What happens if we dereference an uninitialized pointer to struct?
    • It leads to undefined behavior (usually segmentation fault).

Basic Level

  1. What is a pointer to a structure?
  2. How do you access members of a structure using a pointer?
  3. What is the difference between (*ptr).member and ptr->member?

Intermediate Level

  1. How do you dynamically allocate memory for a structure?
  2. What happens if you pass a structure to a function by value vs by pointer?
  3. How can you use pointers to modify structure data inside a function?
  4. Explain how you access an array of structures using a pointer.
  5. Can you pass a pointer to a structure as a function parameter? Show an example.

Advanced Level

  1. What is a self-referential structure? How is it used in linked lists?
  2. How do you handle a structure containing dynamically allocated members?
  3. What happens when you access a structure pointer without initializing it?
  4. What is the memory layout difference between structure and structure pointer?
  5. How do you implement a generic structure pointer that can point to multiple types (with void pointers)?
  6. How is pointer to structure used in memory-mapped I/O (e.g., in embedded systems)?

Real-World/Embedded Examples

  1. Explain how you would map a peripheral register block to a structure pointer.
#define UART_BASE 0x40001000
struct UART {
    volatile uint32_t DATA;
    volatile uint32_t STATUS;
};
#define UART0 ((struct UART *)UART_BASE)
  1. Why is volatile used with structure pointers in embedded systems?

Practice Problems

  1. Write a program to create and print an array of n students using dynamic allocation.
  2. Implement a linked list using self-referential structures.
  3. Write a function that swaps two structure variables using pointers.
  4. Write a function that returns a pointer to a dynamically created structure.
  5. Simulate a memory-mapped I/O register set using structure pointers in C.

You can also Visit other tutorials of Embedded Prep 

Special thanks to @mr-raj for contributing to this article on EmbeddedPrep

Leave a Reply

Your email address will not be published. Required fields are marked *