C Arrays परिचय
In this page:
data_type array_name[size];
data_type array_name[size] = {value1, value2, value3};
array_name[index]; // access an element
Array क्या है?
क्योंकि सारे elements एक contiguous memory block में बैठते हैं, compiler array के starting address और element के index से किसी भी element का address instantly compute कर सकता है, यही वजह है कि array access O(1) है, एक search की ज़रूरत नहीं।
उदाहरण: What is an Array?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int numbers[3] = {10, 20, 30};
// Print formatted text to the screen
printf("%d", numbers[0]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Arrays Declare और Initialize करना
अगर आप इसे explicitly छोड़ दें तो braces के अंदर values की संख्या array का size decide करती है, जैसे int nums[] = {1,2,3} exactly 3 elements का एक array बनाता है।
एक size दिए जाने पर elements को uninitialized छोड़ना उन्हें unpredictable garbage values देता है, zero नहीं।
उदाहरण: Declaring and Initializing Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int nums[] = {1, 2, 3};
// Print formatted text to the screen
printf("%d", nums[2]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Array Elements Access करना
C कोई bounds checking नहीं करता, इसलिए एक 5-element array पर arr[10] पढ़ना या लिखना ठीक compile होता है लेकिन nearby memory corrupt करता है या runtime पर crash करता है -- अगर आपको bounds खुद check करने हों तो हमेशा array के declared size का track अलग से रखें।
उदाहरण: Accessing Array Elements
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Print formatted text to the screen
printf("%d", arr[2]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Array Elements Modify करना
arr[i] = value assign करना सिर्फ उस single memory slot को overwrite करता है; बाकी array untouched रहता है। इस तरह आप array contents को incrementally बनाते हैं, जैसे एक loop के अंदर एक बार में एक entry user input से array भरना।
उदाहरण: Modifying Array Elements
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[3] = {1, 2, 3};
arr[1] = 99;
// Print formatted text to the screen
printf("%d", arr[1]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Arrays के जरिए Loop करना
एक typical traversal loop index 0 से size-1 तक inclusive चलता है -- i < size की बजाय i <= size तक loop करना C में सबसे common off-by-one bugs में से एक है, क्योंकि यह array के actual bounds से एक element आगे पढ़ता है।
उदाहरण: Looping Through Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Loop: repeat while the condition holds
for (int i = 0; i < 5; i++) {
// Print formatted text to the screen
printf("%d ", arr[i]);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- 5 size के array में
arr[5]पढ़ना या लिखना, जब valid indexes 0 से 4 हैं। - एक uninitialized local array इस्तेमाल करना और इसे print करना, जो garbage दिखाता है।
- एक array को दूसरे को
a = b;से assign करना, जो allowed नहीं है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: