Introduction
अब तक हमने arrays और pointers के बीच संबंध को समझा, जहाँ:
- array same type के values store करता है
- pointer किसी variable का address store करता है
लेकिन कई situations में हमें एक ही pointer नहीं, बल्कि कई pointers को together store करना होता है।
ऐसी situation में Array of Pointers का उपयोग किया जाता है।
इसका मतलब है:
एक ऐसा array जिसमें elements normal values न होकर addresses होते हैं।
इस concept का उपयोग तब useful होता है जब:
- कई strings store करनी हों
- multiple variables के addresses handle करने हों
- memory को indirect तरीके से manage करना हो
यह topic थोड़ा conceptual है, लेकिन अगर base clear हो तो बहुत आसान हो जाता है।
Definition
Array of Pointers वह array होता है जिसके हर element में किसी variable का address store होता है।
Basic Syntax
data_type *array_name[size];
Example
int *ptr[3];
Explanation:
ptrएक array है- इसमें 3 elements हैं
- हर element एक integer pointer है
Example 1: Array of Integer Pointers
#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 30;
int *ptr[3];
ptr[0] = &a;
ptr[1] = &b;
ptr[2] = &c;
printf("%d\n", *ptr[0]);
printf("%d\n", *ptr[1]);
printf("%d", *ptr[2]);
return 0;
}
Output:
10
20
30
Explanation:
ptr[0]मेंaका address store है*ptr[0]सेaकी value मिली- इसी तरह बाकी values print हुईं
Example 2: Using Loop with Array of Pointers
#include <stdio.h>
int main()
{
int a = 5, b = 10, c = 15;
int *ptr[3];
int i;
ptr[0] = &a;
ptr[1] = &b;
ptr[2] = &c;
for (i = 0; i < 3; i++)
{
printf("%d ", *ptr[i]);
} return 0;
}
Output:
5 10 15
Explanation:
- loop के through हर pointer की value access की गई
*ptr[i]actual value देता है
Example 3: Array of Pointers with Strings
#include <stdio.h>
int main()
{
char *name[3] = {"Ram", "Shyam", "Mohan"};
printf("%s\n", name[0]);
printf("%s\n", name[1]);
printf("%s", name[2]);
return 0;
}
Output:
Ram
Shyam
Mohan
Explanation:
nameएक array of pointers है- हर element एक string के first character का address store करता है
- इसलिए
%sसे पूरा string print हो गया
Example 4: Accessing Characters through Pointer Array
#include <stdio.h>
int main()
{
char *name[2] = {"Cat", "Dog"};
printf("%c\n", name[0][0]);
printf("%c", name[1][1]);
return 0;
}
Output:
C
o
Explanation:
name[0]→"Cat"का addressname[0][0]→ पहला character = Cname[1][1]→ “Dog” का दूसरा character = o
Difference: Normal Array vs Array of Pointers
| Normal Array | Array of Pointers |
|---|---|
| Values store करता है | Addresses store करता है |
| Example: int arr[3] | Example: int *ptr[3] |
| Direct data access | Indirect data access |
Technical Understanding
Arrays of Pointers का use इन situations में होता है:
- multiple strings handling
- jagged data representation
- dynamic memory references
- efficient string storage
Important Points
- Array of pointers में values नहीं, addresses store होते हैं
int *ptr[3]औरint (*ptr)[3]अलग होते हैं- Strings के साथ यह concept बहुत common है
*ptr[i]से value access होती है
Exam Points
- Array of pointers = array whose elements are pointers
- Syntax important है
- Strings वाले examples बहुत common हैं
- Value और address based access समझना जरूरी है
- Normal array और array of pointers में difference पूछा जाता है