Pointer to Structure in C Programming
Pointer to Structure in C Programming
Introduction to Pointer to Structure in C Programming
Pointer to structure in C programming is used to store the address of a structure variable. It allows efficient access and manipulation of structure members, especially in large programs.
Using pointer to structure in C programming improves performance and memory handling.
What is Pointer to Structure
A pointer to structure is a pointer variable that stores the address of a structure.
Syntax
Example
int id;
char name[20];
};
struct Student s1;
struct Student *ptr = &s1;
Accessing Structure Members Using Pointer
You can access structure members using:
Method 1: Using * and .
Method 2: Using Arrow Operator (->)
The arrow operator is more commonly used.
Example: Pointer to Structure
struct Student {
int id;
char name[20];
};
int main() {
struct Student s1 = {1, “Amit”};
struct Student *ptr = &s1;
printf(“ID: %d\n”, ptr->id);
printf(“Name: %s”, ptr->name);
return 0;
}
Example: Modify Structure Using Pointer
struct Student {
int id;
float marks;
};
int main() {
struct Student s1 = {1, 80.0};
struct Student *ptr = &s1;
ptr->marks = 95.5;
printf(“Updated Marks: %.2f”, s1.marks);
return 0;
}
Pointer to Structure with Functions
struct Student {
int id;
};
void update(struct Student *s) {
s->id = 10;
}
int main() {
struct Student s1 = {1};
update(&s1);
printf(“Updated ID: %d”, s1.id);
return 0;
}
Advantages of Pointer to Structure
Key Benefits
- Efficient memory usage
- Faster access to data
- Useful in functions
- Supports dynamic data structures
Common Mistakes
Avoid These Errors
- Forgetting to initialize pointer
- Incorrect use of * and ->
- Dereferencing null pointer
When to Use Pointer to Structure
Use pointer to structure in C programming when:
- Working with large structures
- Passing structures to functions
- Optimizing memory usage
Start Learning C Programming
Practice pointer to structure programs to improve your understanding of memory and data handling.
Summary
Pointer to structure in C programming stores the address of a structure and allows efficient access using the arrow operator.
FAQs
What is pointer to structure in C programming?
It is a pointer that stores the address of a structure variable.
What is -> operator?
It is used to access structure members using pointer.
Why use pointer to structure?
For efficient memory and performance.
Can we pass structure to function using pointer?
Yes.



