C के Arrays और Functions
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- function के अंदर
sizeofसे array की length मिलने की उम्मीद करना। - array size pass करना भूल जाना, ताकि function अंत से आगे पढ़े।
- यह मान लेना कि arrays pass होने पर copy होते हैं, जब function original पर काम करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: