C का Multidimensional Pointer
In this page:
data_type **pointer_to_pointer = &pointer_name;
data_type (*row_pointer)[columns] = array_name; // pointer to an array row
Pointers to Pointers
एक pointer to a pointer ordinary data के address की बजाय दूसरे pointer variable का address store करता है, और C में दो asterisks से declared होता है, जैसे int **pp;।
Indirection की यह extra layer है जो आपको दूसरे function के अंदर से एक pointer की अपनी value modify करने देती है (सिर्फ जिस data की ओर यह point करता है वह नहीं)।
उदाहरण: Pointers to Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
int *p = &x;
int **pp = &p;
// Print formatted text to the screen
printf("%d", **pp);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Array का Pointer
एक array का pointer एक single element की बजाय एक साथ एक पूरे 2D array की row का address रख सकता है, जो आपको उनका content copy किए बिना पूरी rows को functions में pass करने देता है।
Parameter type को सही declare करना (row के fixed inner dimension से match करते हुए) यही है जो इस तरह के pointer arithmetic को safe बनाता है।
उदाहरण: Pointer to an Array
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int grid[2][3] = {{1,2,3},{4,5,6}};
int (*rowPtr)[3] = grid;
// Print formatted text to the screen
printf("%d", rowPtr[1][2]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
2D Arrays को Dynamically Allocate करना
एक 2D array dynamically बनाने के लिए, पहले हर row represent करने के लिए pointers का एक array allocate करें, फिर हर individual row के लिए एक अलग memory block allocate करें और इसका address corresponding pointer slot में store करें।
यह two-step allocation है जो हर row को runtime पर decided एक size देता है, एक fixed-size 2D array के उलट।
उदाहरण: Dynamically Allocating 2D Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
int rows = 2, cols = 2;
// Allocate memory on the heap
int **arr = malloc(rows * sizeof(int*));
// Loop: repeat while the condition holds
for (int i = 0; i < rows; i++) {
// Allocate memory on the heap
arr[i] = malloc(cols * sizeof(int));
}
arr[0][0] = 5;
// Print formatted text to the screen
printf("%d", arr[0][0]);
// Loop: repeat while the condition holds
for (int i = 0; i < rows; i++) free(arr[i]);
free(arr);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointers से 2D Array Access करना
क्योंकि एक 2D array के elements memory में contiguously बैठते हैं, expression *(*(arr + i) + j) exactly same address compute करता है जो ज़्यादा familiar arr[i][j] करता है — दोनों row i तक चलते हैं, फिर उस row में j elements आगे।
यह equivalence समझना matrices का raw pointer traversal possible बनाता है।
उदाहरण: Accessing 2D Array with Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[2][2] = {{1,2},{3,4}};
// Print formatted text to the screen
printf("%d", *(*(arr + 1) + 0));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamic 2D Arrays Free करना
एक dynamically allocated 2D array free करते समय, आपको row pointers के array को खुद free करने से पहले हर individually allocated row free करना होगा, और opposite order में करना row memory permanently leak करता है क्योंकि आप इसे refer करने वाले इकलौते pointers खो देंगे।
हमेशा inside out free करें।
उदाहरण: Freeing Dynamic 2D Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Declare rows and set it to 2
int rows = 2;
// Allocate memory on the heap
int **arr = malloc(rows * sizeof(int*));
// Loop: repeat while the condition holds
for (int i = 0; i < rows; i++) {
// Allocate memory on the heap
arr[i] = malloc(2 * sizeof(int));
}
// Loop: repeat while the condition holds
for (int i = 0; i < rows; i++) free(arr[i]);
free(arr);
printf("Freed rows first, then the row array");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- declaration में parentheses की वजह से एक array के pointer को pointers के एक array से मिला देना।
- सिर्फ row pointers का array free करना, हर individual row नहीं, एक memory leak cause करते हुए।
- एक dynamically allocated 2D array को गलत तरीके से index करना, जैसे row और column swap करना।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions