- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
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:
- Pointer to Structure Declaration: struct structure_name *ptr; declares a pointer to a structure.
- Accessing Members: Use the arrow operator (->) to access structure members through a pointer.
- 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.
- Dynamic Memory Allocation: You can dynamically allocate memory for structures using malloc() and access members using the pointer.
- 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.
