Introduction
अब तक हमने pointers और structures दोनों को अलग-अलग समझा है:
- Pointer → address store करता है
- Structure → different data types को group करता है
लेकिन real programming में इन दोनों का combination बहुत powerful होता है।
कई situations में हमें:
- structure के address को store करना होता है
- structure के data को indirectly access करना होता है
- large data structures को efficiently handle करना होता है
इसीलिए C language में Structure Pointer का उपयोग किया जाता है।
Structure pointer एक ऐसा pointer होता है जो structure variable का address store करता है।
यह concept important है क्योंकि:
- linked list इसी पर आधारित है
- large structures efficiently pass किए जाते हैं
- memory access fast होता है
Definition
Structure Pointer वह pointer होता है जो किसी structure variable का address store करता है।
Declaration of Structure Pointer
Syntax
struct structure_name *pointer_name;
Example
struct student
{
int roll;
float marks;
};
struct student *p;
Accessing Structure Members using Pointer
दो तरीके होते हैं:
1. Dot + Pointer
(*p).roll
2. Arrow Operator
p->roll
Example 1: Basic Structure Pointer
#include <stdio.h>
struct student
{
int roll;
float marks;
};
int main()
{
struct student s1 = {1, 85.5};
struct student *p;
p = &s1;
printf("%d\n", p->roll);
printf("%.1f", p->marks);
return 0;
}
Output:
1
85.5
Explanation:
- pointer ने structure का address store किया
->से members access किए
Example 2: Using Dot Operator with Pointer
#include <stdio.h>
struct student
{
int roll;
};
int main()
{
struct student s = {10};
struct student *p = &s;
printf("%d", (*p).roll);
return 0;
}
Output:
10
Explanation:
(*p).rollऔरp->rollsame हैं
Example 3: Modify Structure using Pointer
#include <stdio.h>
struct student
{
int marks;
};
int main()
{
struct student s = {50};
struct student *p = &s;
p->marks = 100;
printf("%d", s.marks);
return 0;
}
Output:
100
Explanation:
- pointer के through original value change हुई
Example 4: Array of Structures with Pointer
#include <stdio.h>
struct student
{
int roll;
};
int main()
{
struct student s[2] = {1, 2};
struct student *p = s;
printf("%d\n", p->roll);
printf("%d", (p + 1)->roll);
return 0;
}
Output:
1
2
Explanation:
- pointer array के first element को point करता है
(p+1)→ next structure
Important Concepts
Dot vs Arrow
| Operator | Use |
|---|---|
| . | structure variable |
| -> | pointer to structure |
Technical Understanding
Structure pointer का use:
- linked list
- dynamic structures
- efficient data passing
- memory optimization
Important Points
- structure pointer address store करता है
->operator important है- pointer से structure modify किया जा सकता है
- array of structures में pointer useful है
Exam Points
- Structure pointer = pointer to structure
p->memberबहुत important है(*p).memberऔरp->membersame हैं- pointer से value change हो सकती है
- linked list concept इसी पर आधारित है