🟡 Intermediate  ·  Lesson 20

1D Arrays

Array क्या है?

Array same data type के elements का collection है जो contiguous memory locations में store होते हैं। 10 अलग integer variables declare करने की बजाय, size 10 का एक array use करें।

💡 Memory Layout

int marks[5] के लिए: marks[0], marks[1], marks[2], marks[3], marks[4] — सभी एक साथ memory में। Index हमेशा 0 से शुरू होता है!

Declaration और Initialization

C Language
int marks[5];                           // Declare only
int marks[5] = {90, 85, 78, 92, 88};  // Declare + Initialize
int primes[] = {2, 3, 5, 7, 11};      // Size auto-detect
int zeros[10] = {0};                   // सब 0 initialize

Elements Access करना

C Language
int marks[] = {90, 85, 78, 92, 88};
printf("%d\\n", marks[0]);  // 90 — पहला element
printf("%d\\n", marks[4]);  // 88 — आखिरी element
marks[2] = 95;             // index 2 का value बदला
⚠️ Index Out of Bounds

Size 5 के array में arr[5] access करना undefined behavior है — program crash हो सकता है। C automatically bounds check नहीं करता! हमेशा 0 से size-1 के बीच रहें।

Array Input और Output

C Language
int n = 5, arr[5];
printf("5 numbers daalen:\\n");
for(int i=0; i<n; i++) scanf("%d", &arr[i]);  // Input
for(int i=0; i<n; i++) printf("%d ", arr[i]); // Output

Array Operations

Sum, Average, Max, Min
int marks[] = {85, 92, 78, 96, 70};
int n = sizeof(marks)/sizeof(marks[0]);
int sum=0, max=marks[0], min=marks[0];
for(int i=0; i<n; i++) {
    sum += marks[i];
    if(marks[i]>max) max=marks[i];
    if(marks[i]<min) min=marks[i];
}
printf("Sum=%.d Avg=%.2f Max=%d Min=%d\\n",sum,(float)sum/n,max,min);
Sum=421 Avg=84.20 Max=96 Min=70

सारांश

  • Array = same type के elements का contiguous memory में collection
  • Index 0 से शुरू होता है, size - 1 पर खत्म
  • Declaration: type name[size];
  • Array length: sizeof(arr)/sizeof(arr[0])
  • C automatically bounds check नहीं करता — सावधान रहें!
  • Traverse, search, sort के लिए for loops use करें

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C में one-dimensional array क्या है?
One-dimensional array एक ही type के elements की सूची है जो लगातार memory में रखी जाती है और एक index से access होती है। जैसे int marks[5]; पाँच integers रखता है, और आप हर एक तक marks[0] से marks[4] से पहुँचते हैं।
C में array कैसे declare और initialise करते हैं?
आप इसे type, नाम और size से declare करते हैं, जैसे int a[5];, और साथ ही braces में values से initialise कर सकते हैं, जैसे int a[5] = {1, 2, 3, 4, 5};। अगर आप size से कम values दें, बाकी zero हो जाते हैं।
C में array indexes 0 से क्यों शुरू होते हैं?
Array index असल में array की शुरुआत से offset है, तो पहला element offset 0 पर बैठता है। इसीलिए a[0] पहला item है और size n के array का आख़िरी item a[n-1] है।
C में array out of bounds access करने पर क्या होता है?
C array bounds नहीं जाँचता, तो अंत के पार पढ़ना या लिखना undefined behaviour है — यह garbage लौटा सकता है, दूसरा data खराब कर सकता है, या crash हो सकता है। आपको पक्का करना होगा कि हर index 0 और array size घटा एक के बीच रहे।
C में array की length कैसे निकालते हैं?
जिस array का size compile time पर पता हो, आप उसे sizeof(a) / sizeof(a[0]) से निकाल सकते हैं। यह तब काम नहीं करता जब array pointer में बदल जाए, जैसे function के अंदर, जहाँ आपको size अलग से भेजना होगा।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।