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

C के Arrays और Functions

एक array को एक function को pass करना boxes की original row सौंप देता है, ताकि function उन्हें पढ़ या बदल सके। आप आमतौर पर size भी pass करते हैं, क्योंकि function इसे नहीं जानता।
Syntax
c
return_type function_name(data_type array_param[], int size) {
  // body
}

function_name(array_name, size);

Functions को Arrays Pass करना

जब एक array function boundary पर एक pointer में decay होता है, function को सिर्फ पहले element का address मिलता है -- parameter list में int arr[] जैसे brackets purely cosmetic हैं और int *arr जैसे identically behave करते हैं।

उदाहरण: Passing Arrays to Functions

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

Array Size Pass करना

यह C की सबसे ज़्यादा cite होने वाली design quirks में से एक है: calling function के अंदर sizeof(arr) पूरा array size return करता है, लेकिन called function के अंदर sizeof(arr) एक pointer का size return करता है (ज़्यादातर 64-bit systems पर 8 bytes), इसलिए size को हमेशा explicitly pass करना होता है।

उदाहरण: Passing Array Size

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printAll(int arr[], int size) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < size; i++) {
		// Print formatted text to the screen
		printf("%d ", arr[i]);
	}
}
// Program execution starts in main()
int main() {
	int nums[3] = {10, 20, 30};
	printAll(nums, 3);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Functions के अंदर Arrays Modify करना

यह pass-by-reference जैसा behavior C के दूसरे variable types को treat करने के तरीके से अलग है -- एक int को value से pass करना original को changes से protect करता है, लेकिन arrays हमेशा effectively reference से pass होते हैं क्योंकि सिर्फ उनका address copy होता है।

उदाहरण: Modifying Arrays inside Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void doubleValues(int arr[], int size) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < size; i++) {
		arr[i] *= 2;
	}
}
// Program execution starts in main()
int main() {
	int nums[3] = {1, 2, 3};
	doubleValues(nums, 3);
	// Print formatted text to the screen
	printf("%d %d %d", nums[0], nums[1], nums[2]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multi-dimensional Arrays Pass करना

Compiler को rows के बीच memory में कितना jump करना है (row_size * sizeof(element)) compute करने के लिए column count चाहिए, इसलिए arr[][5] एक parameter की तरह valid है, लेकिन अकेला arr[][] compiler के सही index करने के लिए काफी information नहीं है।

उदाहरण: Passing Multi-dimensional Arrays

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

Arrays को Safely Return करना

Function के अंदर array को static declare करना इसे function के return होने के बाद भी alive रखता है, एक normal local array के उलट जो stack से deallocate हो जाता है -- trade-off यह है कि एक static array उस function की सारी calls में shared होता है, जो careful न रहने पर bugs cause कर सकता है।

उदाहरण: Returning Arrays Safely

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int* getArray() {
	static int arr[3] = {1, 2, 3};
	// Send arr back to the caller
	return arr;
}
// Program execution starts in main()
int main() {
	int *result = getArray();
	// Print formatted text to the screen
	printf("%d", result[0]);
	// 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. function के अंदर sizeof से array की length मिलने की उम्मीद करना।
  2. array size pass करना भूल जाना, ताकि function अंत से आगे पढ़े।
  3. यह मान लेना कि arrays pass होने पर copy होते हैं, जब function original पर काम करता है।
🔒

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.