← Back to C Course | Chapter 6: Arrays & Strings | Lesson 4 of 8

C के Multi-dimensional Arrays

एक multi-dimensional array rows और columns वाली values की एक table है, एक chessboard या spreadsheet की तरह। आप एक item दो numbers से ढूंढते हैं, row और column।
Syntax
c
data_type array_name[rows][columns];

data_type array_name[rows][columns] = {{v1, v2}, {v3, v4}};

array_name[row][column];

Two-Dimensional Arrays

Internally, एक 2D array अभी भी memory के एक contiguous block में row-major order में store होता है, मतलब एक पूरी row अगली row शुरू होने से पहले store होती है -- यही layout है जिसकी वजह से row-by-row loop करना column-by-column से ज़्यादा cache-friendly है।

उदाहरण: Two-Dimensional Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int grid[2][2] = {{1, 2}, {3, 4}};
	// Print formatted text to the screen
	printf("%d", grid[1][0]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

2D Arrays Initialize करना

जब आप {{1,2},{3,4}} जैसे nested braces से initialize करते हैं, हर inner group एक row को order में भरता है; अगर आप किसी row की ज़रूरत से कम values दें, उस row के बाकी elements automatically zero set हो जाते हैं।

उदाहरण: Initializing 2D Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int grid[2][2] = {{1, 2}, {3}};
	// Print formatted text to the screen
	printf("%d", grid[1][1]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

2D Arrays में Elements Access करना

grid[2][3] 'row 2, column 3' की तरह पढ़ा जाता है -- order मिला देना bugs का एक frequent source है, खासकर mathematical matrix notation (जिसे कुछ fields row-first लिखते हैं, दूसरे column-first) और code के बीच translate करते समय।

उदाहरण: Accessing Elements in 2D Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int grid[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
	// Print formatted text to the screen
	printf("%d", grid[2][3]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

2D Arrays में Elements Modify करना

1D arrays की तरह ही, grid[i][j] update करना सिर्फ उस single cell को बदलता है; loop करने या उस row की contiguous memory पर memcpy() इस्तेमाल करने के अलावा एक पूरी row को एक साथ reassign करने का कोई built-in तरीका नहीं है।

उदाहरण: Modifying Elements in 2D Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int grid[2][2] = {{1, 2}, {3, 4}};
	grid[0][1] = 99;
	// Print formatted text to the screen
	printf("%d", grid[0][1]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

2D Arrays के जरिए Loop करना

Outer loop variable आमतौर पर row represent करता है और inner loop column (या उल्टा, जब तक आप consistent रहें) -- किसी भी dimension के लिए loop bounds गलत करना आसान है और यह data skip करेगा या bounds से बाहर पढ़ेगा।

उदाहरण: Looping Through 2D Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int grid[2][2] = {{1, 2}, {3, 4}};
	// Loop: repeat while the condition holds
	for (int row = 0; row < 2; row++) {
		// Loop: repeat while the condition holds
		for (int col = 0; col < 2; col++) {
			// Print formatted text to the screen
			printf("%d ", grid[row][col]);
		}
	}
	// Return 0 to signal that the program finished successfully
	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. row और column indexes swap करना, जैसे a[col][row]।
  2. एक 2D array को function में pass करते समय column size छोड़ना, जो ज़रूरी है।
  3. यह मान लेना कि एक partially initialized 2D array random है, जब missing elements 0 हैं।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.