Pointers to Structures

A pointer to a structure is simply a variable that stores the memory address of a structure variable. Just like you can have pointers to integers or characters, you can have pointers to any structure type.

Declaring Pointers to Structures

The syntax for declaring a pointer to a structure is:

struct structure_name *pointer_name;

Here's a practical example:

#include <stdio.h>

struct Student {
    char name[50];
    int age;
    float grade;
};

int main() {
    struct Student student1 = {"Alice", 20, 85.5};
    struct Student *ptr;  // Pointer to Student structure
    
    ptr = &student1;  // Point to student1
    
    return 0;
}

Accessing Structure Members Through Pointers

There are two ways to access structure members using pointers:

Method 1: Using the Arrow Operator (->)

The arrow operator is the most common and convenient way:

#include <stdio.h>

struct Student {
    char name[50];
    int age;
    float grade;
};

int main() {
    struct Student student1 = {"Bob", 22, 78.9};
    struct Student *ptr = &student1;
    
    // Accessing members using arrow operator
    printf("Name: %s\n", ptr->name);
    printf("Age: %d\n", ptr->age);
    printf("Grade: %.1f\n", ptr->grade);
    
    return 0;
}

Method 2: Using Dereference Operator (*) with Dot Operator

#include <stdio.h>

struct Student {
    char name[50];
    int age;
    float grade;
};

int main() {
    struct Student student1 = {"Charlie", 19, 92.3};
    struct Student *ptr = &student1;
    
    // Accessing members using dereference operator
    printf("Name: %s\n", (*ptr).name);
    printf("Age: %d\n", (*ptr).age);
    printf("Grade: %.1f\n", (*ptr).grade);
    
    return 0;
}

Modifying Structure Members Through Pointers

You can modify structure members using pointers:

#include <stdio.h>
#include <string.h>

struct Student {
    char name[50];
    int age;
    float grade;
};

int main() {
    struct Student student1 = {"David", 21, 75.0};
    struct Student *ptr = &student1;
    
    printf("Before modification:\n");
    printf("Name: %s, Age: %d, Grade: %.1f\n", 
           ptr->name, ptr->age, ptr->grade);
    
    // Modifying through pointer
    strcpy(ptr->name, "David Smith");
    ptr->age = 22;
    ptr->grade = 88.5;
    
    printf("After modification:\n");
    printf("Name: %s, Age: %d, Grade: %.1f\n", 
           ptr->name, ptr->age, ptr->grade);
    
    return 0;
}

Dynamic Memory Allocation for Structures

One of the most powerful uses of structure pointers is with dynamic memory allocation:

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

struct Student {
    char name[50];
    int age;
    float grade;
};

int main() {
    struct Student *ptr;
    
    // Allocate memory for one Student structure
    ptr = (struct Student*)malloc(sizeof(struct Student));
    
    if (ptr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }
    
    // Initialize the structure
    strcpy(ptr->name, "Emma");
    ptr->age = 20;
    ptr->grade = 91.2;
    
    printf("Student Info:\n");
    printf("Name: %s\n", ptr->name);
    printf("Age: %d\n", ptr->age);
    printf("Grade: %.1f\n", ptr->grade);
    
    // Don't forget to free the memory!
    free(ptr);
    
    return 0;
}

Comparison: Arrow vs Dot-Dereference

MethodSyntaxWhen to Use
Arrow Operatorptr->memberMost common, cleaner syntax
Dot-Dereference(*ptr).memberWhen you need to emphasize dereferencing

Why Use Pointers to Structures?

  1. Memory Efficiency: Passing pointers instead of entire structures saves memory
  2. Performance: Faster function calls since you're not copying large structures
  3. Dynamic Allocation: Create structures at runtime based on program needs
  4. Linked Data Structures: Essential for creating linked lists, trees, etc.

Think of it like this: instead of photocopying an entire book to share information, you just tell someone the library and shelf number where they can find it. That's what pointers do - they give you the "address" where the data lives!

Key Points to Remember

  • Always initialize pointers before using them
  • Use -> operator for accessing members through pointers
  • Don't forget to free dynamically allocated memory
  • Pointers to structures are essential for building complex data structures
  • Structure pointers make your programs more efficient and flexible