C Programs Tutorials | IT Developer
IT Developer

C Programming - C Pointer to Structures



Share with a Friend

C Programming - C Pointer to Structures

C Pointer to Structures

In C, a pointer to a structure is a pointer that points to the memory location where a structure is stored. This is useful when you want to pass large structures to functions efficiently without copying the entire structure. Instead, you can pass a pointer to the structure.

A pointer to a structure allows you to manipulate the structure's members directly by dereferencing the pointer.

Syntax to Declare a Pointer to a Structure

To declare a pointer to a structure, you would use the following syntax:

C

struct structure_name *pointer_name;

  • structure_name: The name of the structure.
  • pointer_name: The name of the pointer.

Example 1: Pointer to a Structure

Let’s start with a basic example where we declare a structure and a pointer to that structure.

C

#include <stdio.h>

// Define a structure

struct Person {

    char name[50];

    int age;

};

int main() {

    // Declare a structure variable

    struct Person person1 = {"Alice", 25};

    // Declare a pointer to the structure

    struct Person *ptr;

    // Point to the structure variable

    ptr = &person1;

    // Access structure members using pointer dereferencing

    printf("Name: %s\n", ptr->name);  // Use '->' to access members

    printf("Age: %d\n", ptr->age);

    return 0;

}

Output:

Name: Alice

Age: 25

Explanation:

  • struct Person *ptr; declares a pointer ptr that can point to a struct Person.
  • ptr = &person1; makes ptr point to the memory location of person1.
  • To access the members of the structure via the pointer, we use the arrow operator (->). This is because the pointer holds the address of the structure.

Example 2: Pointer to a Structure in a Function

When you pass a structure to a function, you can pass a pointer to the structure to avoid copying the entire structure. This method is efficient, especially for large structures.

C

#include <stdio.h>

struct Person {

    char name[50];

    int age;

};

// Function to print structure data

void printPerson(struct Person *p) {

    printf("Name: %s\n", p->name);

    printf("Age: %d\n", p->age);

}

int main() {

    struct Person person1 = {"Bob", 30};

    // Passing pointer to the function

    printPerson(&person1);

    return 0;

}

Output:

Name: Bob

Age: 30

Explanation:

  • printPerson takes a pointer to a struct Person as its parameter.
  • In main(), we pass the address of person1 using &person1 to the function.
  • Inside the function, we use p->name and p->age to access the structure members via the pointer.

Example 3: Dynamic Memory Allocation for Structures

You can dynamically allocate memory for a structure using malloc() and create a pointer to that dynamically allocated memory.

C

#include <stdio.h>

#include <stdlib.h>

struct Person {

    char name[50];

    int age;

};

int main() {

    // Dynamically allocate memory for a structure

    struct Person *ptr = (struct Person *)malloc(sizeof(struct Person));

    if (ptr == NULL) {

        printf("Memory allocation failed!\n");

        return 1;  // Exit the program if memory allocation fails

    }

    // Assign values to structure members through the pointer

    strcpy(ptr->name, "John");

    ptr->age = 40;

    // Print structure members

    printf("Name: %s\n", ptr->name);

    printf("Age: %d\n", ptr->age);

    // Free the dynamically allocated memory

    free(ptr);

    return 0;

}

Output:

Name: John

Age: 40

Explanation:

  • We dynamically allocate memory for a structure using malloc().
  • The pointer ptr is used to assign values to the structure members.
  • After using the pointer, we release the memory with free(ptr) to prevent memory leaks.

Example 4: Array of Pointers to Structures

You can also create an array of pointers to structures, which is useful when dealing with multiple structures dynamically.

C

#include <stdio.h>

struct Person {

    char name[50];

    int age;

};

int main() {

    // Declare an array of pointers to struct Person

    struct Person *arr[2];

    // Dynamically allocate memory for each structure

    arr[0] = (struct Person *)malloc(sizeof(struct Person));

    arr[1] = (struct Person *)malloc(sizeof(struct Person));

    if (arr[0] == NULL || arr[1] == NULL) {

        printf("Memory allocation failed!\n");

        return 1;

    }

    // Assign values to structures

    strcpy(arr[0]->name, "Alice");

    arr[0]->age = 25;

    strcpy(arr[1]->name, "Bob");

    arr[1]->age = 30;

    // Print structure members

    printf("Person 1: %s, Age: %d\n", arr[0]->name, arr[0]->age);

    printf("Person 2: %s, Age: %d\n", arr[1]->name, arr[1]->age);

    // Free dynamically allocated memory

    free(arr[0]);

    free(arr[1]);

    return 0;

}

Output:

Person 1: Alice, Age: 25

Person 2: Bob, Age: 30

Explanation:

  • We create an array arr of pointers to struct Person and dynamically allocate memory for each structure.
  • We then assign values to each structure and access them using the pointer.

Key Points:

  1. Pointer to Structure Declaration: struct structure_name *ptr; declares a pointer to a structure.
  2. Accessing Members: Use the arrow operator (->) to access structure members through a pointer.
  3. Passing Pointers to Functions: When passing structures to functions, it is more efficient to pass a pointer to the structure rather than the structure itself.
  4. Dynamic Memory Allocation: You can dynamically allocate memory for structures using malloc() and access members using the pointer.
  5. Array of Pointers: You can create an array of pointers to structures, useful for managing multiple instances of a structure.

Conclusion:

Pointers to structures are an important feature in C programming. They help in efficient memory management and allow passing large structures to functions without copying them. Understanding how to use pointers with structures is essential for writing modular, efficient, and flexible programs.