← Back to C Course | Chapter 12: Advanced Topics | Lesson 8 of 20

C का Multidimensional Pointer

Multidimensional pointers आपको memory addresses के जरिए data की tables के साथ काम करने में मदद करते हैं, rows और columns तक एक map follow करने जैसा। आप program चलते समय एक table बना और free भी कर सकते हैं।
Syntax
c
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

c
// 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;
}

एक 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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. declaration में parentheses की वजह से एक array के pointer को pointers के एक array से मिला देना।
  2. सिर्फ row pointers का array free करना, हर individual row नहीं, एक memory leak cause करते हुए।
  3. एक dynamically allocated 2D array को गलत तरीके से index करना, जैसे row और column swap करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.