PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम 1D Arrays in C: Declaration, Indexing, Bounds and Memory (1D एरे: इंडेक्सिंग 0 से N-1, शून्य सीमा जाँच और मेमोरी) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: A 1D array in C stores fixed-size homogeneous elements in contiguous RAM indexed from 0 to N-1, offering O(1) offset address access with zero automatic runtime bounds checking.
अध्याय सार: C में 1D एरे एक ही डेटा टाइप के तत्वों को लगातार (Contiguous) मेमोरी में 0 से N-1 इंडेक्स तक संग्रहीत करता है, जहाँ बिना किसी ऑटोमैटिक बाउंड चेकिंग के O(1) गति से एक्सेस मिलता है।
1D Arrays in C: Declaration, Indexing, Bounds and Memory: Detailed Notes & Architecture (1D एरे: इंडेक्सिंग 0 से N-1, शून्य सीमा जाँच और मेमोरी)
1. Declaration, Memory Contiguity & Initialization Patterns (डिक्लेरेशन, लगातार मेमोरी व इनिशियलाइजेशन नियम)
[EN] Declaring 'int a[5];' reserves 5 * sizeof(int) contiguous bytes (20 bytes on 32/64-bit systems). Uninitialized local arrays contain garbage values, whereas partial brace initialization such as 'int a[5] = {10, 20};' sets remaining indices a[2..4] automatically to 0 (so 'int a[5] = {0};' zeroes all elements). When all elements are listed, size may be omitted: 'int b[] = {4, 7, 9};' deduces length 3. C99 also introduced Designated Initializers like 'int c[5] = {[3] = 99};' and Variable Length Arrays (VLAs).
[HI] C में 'int a[5];' लिखने पर लगातार 5 * sizeof(int) बाइट्स मेमोरी आरक्षित होती है। बिना इनिशियलाइज किए गए लोकल एरे में गार्बेज मान रहते हैं, लेकिन आंशिक इनिशियलाइजेशन (जैसे int a[5] = {10, 20};) में बचे हुए सभी स्थान स्वतः 0 हो जाते हैं। संपूर्ण मान देने पर आकार छोड़ा जा सकता है (int b[] = {4, 7, 9};)। C99 में डेजिग्नेटेड इनिशियलाइजर ([3] = 99) व वेरिएबल लेंथ एरे (VLA) की सुविधा भी दी गई।
2. Zero-Based Indexing & Physical Address Arithmetic (0-आधारित इंडेक्सिंग व मेमोरी एड्रेस गणना)
[EN] C uses 0-based indexing because the index directly represents element offset from the base address (&arr[0]). For an array of element size S bytes starting at Base_Address, element arr[i] resides at physical byte address Base_Address + i * S. By C language definition, arr[i] is syntactic shorthand for *(arr + i). Because addition is commutative (*(arr + i) == *(i + arr)), quirky expressions like 2[arr] compile and evaluate identically to arr[2]!
[HI] C में इंडेक्स 0 से N-1 तक चलता है क्योंकि इंडेक्स सीधे बेस एड्रेस (&arr[0]) से दूरी (Offset) दर्शाता है। यदि प्रत्येक तत्व का आकार S बाइट हो, तो arr[i] का मेमोरी पता Base_Address + i * S होता है। C परिभाषा के अनुसार arr[i] वास्तव में *(arr + i) के बराबर है, जिसके कारण 2[arr] लिखना भी arr[2] जैसा ही परिणाम देता है।
3. Zero Runtime Bounds Checking & Buffer Overflow Traps (शून्य सीमा जाँच व बफर ओवरफ्लो खतरा)
[EN] Prioritizing execution performance, C performs zero automatic runtime bounds checking on array subscripts. Accessing or writing arr[-1] or arr[N] compiles without error and triggers Undefined Behavior—silently reading garbage or corrupting adjacent stack variables, saved frame pointers, or return addresses. This lack of bounds enforcement is the historical root cause of classic Stack Buffer Overflow security vulnerabilities.
[HI] अधिकतम गति बनाए रखने के लिए C रनटाइम में एरे सीमा की कोई ऑटोमैटिक जांच (Bounds Checking) नहीं करता। arr[-1] या arr[N] को पढ़ना/लिखना बिना किसी रनटाइम एरर के अनडिफाइंड बिहेवियर उत्पन्न करता है, जिससे पास के लोकल वेरिएबल या रिटर्न एड्रेस खराब होकर गंभीर बफर ओवरफ्लो सुरक्षा दोष बन सकता है।
4. sizeof Element Count Formula & Copying Restrictions (sizeof से आकार गणना व एरे कॉपी प्रतिबंध)
[EN] In the scope where the array is declared, sizeof(arr) returns total allocated bytes (N * sizeof(type)), so element count is safely computed via 'sizeof(arr) / sizeof(arr[0])'. Because an array name acts as a non-modifiable lvalue, direct array-to-array assignment like 'b = a;' or comparison 'if (a == b)' (which compares addresses, not contents) is invalid—copying requires a for-loop or memcpy() from <string.h>.
[HI] जिस स्कोप में एरे बना है वहाँ sizeof(arr) कुल बाइट्स देता है, अतः तत्वों की संख्या sizeof(arr) / sizeof(arr[0]) से निकाली जाती है। C में एरे का नाम अपरिवर्तनीय lvalue होने से 'b = a;' लिखकर सीधे कॉपी करना अवैध (कम्पाइल एरर) है; कॉपी करने के लिए लूप या memcpy() का उपयोग करना पड़ता है।
Syntax, Formula & Technical Example (1D Arrays in C: Declaration, Indexing, Bounds and Memory)
#include <stdio.h>
int main(void) {
int a[5] = {10, 20}; // Remaining elements a[2], a[3], a[4] become 0
int n = sizeof(a) / sizeof(a[0]); // 20 / 4 = 5 elements
for (int i = 0; i < n; i++) {
printf("a[%d]=%d @ %p\n", i, a[i], (void *)&a[i]);
}
return 0;
}
1D Arrays in C: Declaration, Indexing, Bounds and Memory Quick Reference & Comparison Table
| 1D Array Operation | C Technical Behavior | Key Exam Fact |
|---|---|---|
| Valid Index Range (size N) | 0 through N - 1 (offset from Base_Address) | arr[N] is out of bounds (Undefined Behavior) |
| Partial Init: int a[5] = {7}; | a[0] = 7, and a[1..4] automatically become 0 | int a[5] = {0}; clears entire array |
| Physical Address of arr[i] | Base_Address + i * sizeof(element_type) | O(1) constant-time random access |
| Direct Copy Assignment (b = a) | Compile-time error (array is non-modifiable lvalue) | Must copy via loop or memcpy() |
1D Arrays in C: Declaration, Indexing, Bounds and Memory Top 20 Bilingual MCQs with Answers
Q1. For an array declared as 'double scores[12];' on a system where sizeof(double) == 8, how many total bytes does scores occupy?
(जिस सिस्टम पर sizeof(double) == 8 बाइट हो, वहाँ 'double scores[12];' एरे कुल कितने बाइट मेमोरी घेरेगा?)
(A) 20 bytes
(B) 96 bytes
(C) 12 bytes
(D) 48 bytes
Correct Answer: (B)
[EN] Total array size equals element count * element size = 12 * 8 = 96 bytes.
[HI] कुल आकार = तत्वों की संख्या * एक तत्व का आकार = 12 * 8 = 96 बाइट्स होगा।
Q2. If 'int arr[10];' starts at base memory address 2000 and sizeof(int) is 4 bytes, what is the address of arr[6]?
(यदि 'int arr[10];' का बेस एड्रेस 2000 हो और sizeof(int) = 4 बाइट हो, तो arr[6] का मेमोरी एड्रेस क्या होगा?)
(A) 2028
(B) 2024
(C) 2006
(D) 2040
Correct Answer: (B)
[EN] Address(arr[i]) = Base + i * sizeof(int) = 2000 + 6 * 4 = 2024.
[HI] Address(arr[6]) = 2000 + 6 * 4 = 2024 होगा।
Q3. In 'int x[5] = {12, 34};' inside main(), what value does x[4] hold?
(main() के भीतर 'int x[5] = {12, 34};' घोषित करने पर x[4] का मान क्या होगा?)
(A) Garbage value
(B) Compile-time error
(C) 34
(D) 0
Correct Answer: (D)
[EN] When at least one initializer is supplied, C automatically zero-initializes all remaining unspecified elements.
[HI] आंशिक इनिशियलाइजेशन होने पर शेष सभी अनिर्दिष्ट तत्व C द्वारा स्वतः 0 कर दिए जाते हैं।
Q4. In 'int y[5];' inside main() without any initializer, what do elements y[0..4] initially contain?
(main() के अंदर बिना किसी इनिशियलाइजेशन के 'int y[5];' घोषित करने पर उसके तत्वों में प्रारंभ में क्या रहता है?)
(A) Indeterminate (Garbage) values
(B) All zeros
(C) NULL
(D) Indices 0, 1, 2, 3, 4
Correct Answer: (A)
[EN] Local automatic arrays without an initializer receive uninitialized stack memory holding leftover garbage bytes.
[HI] बिना इनिशियलाइजर वाले लोकल ऑटोमैटिक एरे में प्रारंभ में अनिश्चित (Garbage) मान रहते हैं।
Q5. What does C do if a program accesses arr[10] on an array declared as 'int arr[10];'?
(यदि 'int arr[10];' में प्रोग्राम arr[10] को पढ़े या लिखे, तो C भाषा क्या करती है?)
(A) Silently wraps index around to arr[0]
(B) Performs no automatic bounds check, triggering Undefined Behavior
(C) Throws ArrayIndexOutOfBoundsException at runtime
(D) Rejects compilation with an out-of-bounds syntax error
Correct Answer: (B)
[EN] C performs zero runtime bounds checking; valid indices are 0..9, so arr[10] causes Undefined Behavior (often memory corruption).
[HI] C में रनटाइम बाउंड चेकिंग नहीं होती; 0 से 9 के बाहर arr[10] एक्सेस करने पर अनडिफाइंड बिहेवियर होता है।
Q6. Which expression evaluates identically to 'arr[3]' in standard C?
(मानक C में इनमें से कौन सा एक्सप्रेशन 'arr[3]' के बिल्कुल बराबर मान देता है?)
(A) 3[arr]
(B) arr + 3
(C) *arr + 3
(D) &arr[3]
Correct Answer: (A)
[EN] Because arr[3] == *(arr + 3) == *(3 + arr), C treats 3[arr] identically to arr[3] (while *arr + 3 adds 3 to arr[0]).
[HI] arr[3] का अर्थ *(arr + 3) होता है जो *(3 + arr) अर्थात 3[arr] के समान है, जबकि *arr + 3 पहले तत्व में 3 जोड़ता है।
Q7. How do you compute the number of elements in a local 1D array 'int data[] = {5, 8, 2, 9, 1};'?
(लोकल 1D एरे 'int data[] = {5, 8, 2, 9, 1};' में तत्वों की कुल संख्या कैसे निकाली जाती है?)
(A) strlen(data)
(B) sizeof(data) * sizeof(int)
(C) sizeof(data) / sizeof(data[0])
(D) sizeof(data)
Correct Answer: (C)
[EN] sizeof(data) gives 20 bytes and sizeof(data[0]) gives 4 bytes, yielding 20 / 4 = 5 elements.
[HI] sizeof(data) कुल बाइट्स (20) देता है जिसे एक तत्व के आकार sizeof(data[0]) (4) से भाग देने पर संख्या 5 मिलती है।
Q8. What happens when compiling 'int a[3] = {1, 2, 3}, b[3]; b = a;' in C?
(C में 'int a[3] = {1, 2, 3}, b[3]; b = a;' लिखकर कम्पाइल करने पर क्या होगा?)
(A) Only a[0] copies into b[0]
(B) Compile-time error: assignment to expression with array type
(C) b points to a as an alias
(D) All 3 integers copy from a to b
Correct Answer: (B)
[EN] Arrays are non-modifiable lvalues in C, so direct assignment b = a is illegal; elements must be copied individually or via memcpy.
[HI] C में एरे पर सीधा असाइनमेंट (b = a) अवैध है और कम्पाइल एरर देता है।
Q9. What happens if you declare 'int nums[] = {10, 20, 30, 40};' without specifying size inside brackets?
(यदि ब्रैकेट में आकार न लिखकर 'int nums[] = {10, 20, 30, 40};' लिखा जाए तो क्या होगा?)
(A) Array dynamically resizes at runtime
(B) Compiler automatically deduces size 4 (16 bytes for 4-byte int)
(C) Array size defaults to 0
(D) Compile-time error because size is missing
Correct Answer: (B)
[EN] When an initializer list is present, C counts the initializers and sizes the array accordingly.
[HI] इनिशियलाइजेशन सूची होने पर कंपाइलर स्वतः तत्व गिनकर आकार 4 तय कर लेता है।
Q10. In C99 and later, what does 'int a[5] = {[1] = 10, [4] = 40};' initialize the array to?
(C99 तथा बाद के मानक में 'int a[5] = {[1] = 10, [4] = 40};' से एरे में क्या मान भरते हैं?)
(A) {0, 10, 0, 0, 40}
(B) Syntax error
(C) {1, 4, 0, 0, 0}
(D) {10, 40, 0, 0, 0}
Correct Answer: (A)
[EN] C99 designated initializers place 10 at index 1, 40 at index 4, and zero-fill every omitted index.
[HI] डेजिग्नेटेड इनिशियलाइजर इंडेक्स 1 पर 10 व इंडेक्स 4 पर 40 रखकर शेष सभी तत्वों को 0 कर देता है।
Q11. What happens in C if you write 'int a[3] = {10, 20, 30, 40};' (more initializers than declared capacity)?
(यदि C में क्षमता से अधिक मान दे दिए जाएँ (जैसे 'int a[3] = {10, 20, 30, 40};'), तो क्या होता है?)
(A) Compiler issues a warning/error for excess elements in array initializer and discards or rejects 40
(B) Array automatically expands to size 4
(C) 40 overwrites a[0]
(D) Program adds 30 + 40 into a[2]
Correct Answer: (A)
[EN] Providing excess elements violates constraints; modern GCC/Clang emit an excess-elements warning or error (-Werror).
[HI] क्षमता से अधिक मान देने पर कंपाइलर चेतावनी/त्रुटि देता है क्योंकि एरे आकार 3 पर स्थिर रहता है।
Q12. Why is accessing any element arr[i] in a 1D array an O(1) constant-time operation?
(1D एरे में किसी भी तत्व arr[i] को एक्सेस करना O(1) समय की प्रक्रिया क्यों होती है?)
(A) Every element carries a pointer to the next element
(B) Contiguous memory allows direct offset calculation Base + i * size without traversing earlier elements
(C) CPU searches elements sequentially using Binary Search
(D) The compiler stores every index in a hash table
Correct Answer: (B)
[EN] Random access via arithmetic multiplication takes constant CPU cycles regardless of array size N.
[HI] लगातार मेमोरी होने से बेस पते में i * साइज जोड़कर सीधे किसी भी तत्व तक पहुंचा जा सकता है।
Q13. What security vulnerability directly arises from C's lack of runtime array bounds checking when writing past arr[N-1]?
(C में रनटाइम बाउंड चेकिंग न होने के कारण arr[N-1] के आगे लिखने से कौन सा गंभीर सुरक्षा दोष उत्पन्न होता है?)
(A) ARP Spoofing
(B) Buffer Overflow
(C) Cross-Site Request Forgery
(D) SQL Injection
Correct Answer: (B)
[EN] Writing beyond an array's stack allocation overwrites adjacent variables or return addresses, enabling Buffer Overflow exploits.
[HI] एरे सीमा से बाहर लिखने पर स्टैक की बगल वाली मेमोरी व रिटर्न एड्रेस खराब हो जाते हैं, जिसे बफर ओवरफ्लो कहते हैं।
Q14. Given 'int a[4] = {2, 4, 6, 8};', what does 'printf("%d", *a + *(a + 2));' output?
(यदि 'int a[4] = {2, 4, 6, 8};' हो, तो 'printf("%d", *a + *(a + 2));' क्या प्रिंट करेगा?)
(A) 8
(B) 12
(C) 6
(D) 10
Correct Answer: (A)
[EN] *a equals a[0] (2) and *(a + 2) equals a[2] (6); 2 + 6 = 8.
[HI] *a का मान a[0]=2 और *(a+2) का मान a[2]=6 है, अतः 2 + 6 = 8 प्रिंट होगा।
Q15. Which statement is NOT true about 1D arrays in C?
(C में 1D एरे के बारे में इनमें से कौन सा कथन असत्य है?)
(A) Elements are stored in contiguous memory addresses
(B) Valid indices for size N range from 0 to N - 1
(C) A single 1D array in C can simultaneously mix int, float, and char element types
(D) Array size must be positive (>= 1) in standard ISO C
Correct Answer: (C)
[EN] An array is a homogeneous collection where every element shares the exact same data type and byte width.
[HI] एरे एक समरूप (Homogeneous) संग्रह है जिसके सभी तत्व एक ही डेटा टाइप के होते हैं, अलग-अलग टाइप मिश्रित नहीं हो सकते।
Q16. What happens when comparing two identical arrays 'int a[3] = {1,2,3}, b[3] = {1,2,3};' using 'if (a == b)'?
(दो समान एरे 'int a[3] = {1,2,3}, b[3] = {1,2,3};' की तुलना 'if (a == b)' से करने पर क्या परिणाम आएगा?)
(A) It compares only a[0] == b[0]
(B) Condition evaluates to True (1) because elements match
(C) Compile-time syntax error
(D) Condition evaluates to False (0) because a and b decay to two different memory addresses
Correct Answer: (D)
[EN] a == b compares &a[0] == &b[0], which are distinct addresses in RAM; use memcmp or a loop to compare contents.
[HI] a == b दोनों एरे के अलग-अलग बेस एड्रेस की तुलना करता है जो असमान होने से False (0) देता है।
Q17. Within 1D Arrays in C: Declaration, Indexing, Bounds and Memory, which 1d array operation directly maps to: "0 through N - 1 (offset from Base_Address)"?
(1D Arrays in C: Declaration, Indexing, Bounds and Memory में कौन सा 1D Array Operation सीधे "0 through N - 1 (offset from Base_Address)" से संबंधित है?)
(A) Valid Index Range (size N)
(B) Physical Address of arr[i]
(C) Direct Copy Assignment (b = a)
(D) Partial Init: int a[5] = {7};
Correct Answer: (A)
[EN] In 1D Arrays in C: Declaration, Indexing, Bounds and Memory, 'Valid Index Range (size N)' handles '0 through N - 1 (offset from Base_Address)' (arr[N] is out of bounds (Undefined Behavior)).
[HI] 1D Arrays in C: Declaration, Indexing, Bounds and Memory में 'Valid Index Range (size N)' का कार्य/विवरण '0 through N - 1 (offset from Base_Address)' (arr[N] is out of bounds (Undefined Behavior)) है।
Q18. Which option accurately describes 'Partial Init: int a[5] = {7};' in 1D Arrays in C: Declaration, Indexing, Bounds and Memory?
(1D Arrays in C: Declaration, Indexing, Bounds and Memory के संदर्भ में 'Partial Init: int a[5] = {7};' की सही विशेषता कौन सी है?)
(A) Compile-time error (array is non-modifiable lvalue)
(B) Base_Address + i * sizeof(element_type)
(C) 0 through N - 1 (offset from Base_Address)
(D) a[0] = 7, and a[1..4] automatically become 0
Correct Answer: (D)
[EN] 'Partial Init: int a[5] = {7};' provides 'a[0] = 7, and a[1..4] automatically become 0' — key point: int a[5] = {0}; clears entire array.
[HI] 'Partial Init: int a[5] = {7};' का मुख्य विवरण 'a[0] = 7, and a[1..4] automatically become 0' है (int a[5] = {0}; clears entire array)।
Q19. In 1D Arrays in C: Declaration, Indexing, Bounds and Memory, identify the correct parameter for the specification: "Base_Address + i * sizeof(element_type)".
(1D Arrays in C: Declaration, Indexing, Bounds and Memory में "Base_Address + i * sizeof(element_type)" विवरण किस विकल्प के लिए सही है?)
(A) Partial Init: int a[5] = {7};
(B) Valid Index Range (size N)
(C) Physical Address of arr[i]
(D) Direct Copy Assignment (b = a)
Correct Answer: (C)
[EN] 'Physical Address of arr[i]' corresponds to 'Base_Address + i * sizeof(element_type)' (O(1) constant-time random access).
[HI] 'Physical Address of arr[i]' का संबंध 'Base_Address + i * sizeof(element_type)' (O(1) constant-time random access) से है।
Q20. Considering 1D Arrays in C: Declaration, Indexing, Bounds and Memory, what key behavior defines 'Direct Copy Assignment (b = a)'?
(1D Arrays in C: Declaration, Indexing, Bounds and Memory में 'Direct Copy Assignment (b = a)' की मुख्य भूमिका या मान क्या है?)
(A) a[0] = 7, and a[1..4] automatically become 0
(B) Compile-time error (array is non-modifiable lvalue)
(C) 0 through N - 1 (offset from Base_Address)
(D) Base_Address + i * sizeof(element_type)
Correct Answer: (B)
[EN] 'Direct Copy Assignment (b = a)' represents 'Compile-time error (array is non-modifiable lvalue)' (Must copy via loop or memcpy()).
[HI] 'Direct Copy Assignment (b = a)' का अर्थ/मान 'Compile-time error (array is non-modifiable lvalue)' है (Must copy via loop or memcpy())।
1D Arrays in C: Declaration, Indexing, Bounds and Memory — Exam FAQs
Q1: Why did Dennis Ritchie start array indexing at 0 instead of 1 in C?
C भाषा में एरे इंडेक्सिंग 1 के बजाय 0 से क्यों शुरू की गई?
[EN] In C (inherited from BCPL/B), the index i is a direct byte-stride offset from the base pointer: arr[i] means *(arr + i). Starting at 0 ensures the first element resides at offset 0 (*arr), avoiding an extra subtraction of 1 on every single array lookup.
[HI] C में इंडेक्स i बेस एड्रेस से ऑफसेट दूरी दर्शाता है: arr[i] का अर्थ *(arr + i) है। 0 से शुरू करने पर पहले तत्व पर कोई अतिरिक्त -1 घटाव नहीं करना पड़ता जिससे एक्सेस तेज रहता है।
Q2: Can a C array be resized dynamically once declared as int a[10];?
क्या एक बार 'int a[10];' घोषित होने के बाद C एरे का आकार रनटाइम पर बढ़ाया जा सकता है?
[EN] No. Fixed arrays (and even C99 VLAs) have constant size throughout their lifetime. If you need a growable array, allocate on the heap with malloc() and resize using realloc().
[HI] नहीं। स्टैक पर बने सामान्य एरे का आकार स्थिर रहता है। आवश्यकतानुसार आकार बढ़ाने या घटाने के लिए हीप मेमोरी पर malloc() व realloc() का उपयोग किया जाता है।
Q3: What are Variable Length Arrays (VLAs) in C99 and why are they risky?
C99 में Variable Length Arrays (VLA) क्या हैं और उनमें क्या जोखिम है?
[EN] C99 allows local array bounds using runtime variables (e.g., int a[n];), allocated on the stack. However, large n can silently trigger Stack Overflow with no error return, which is why VLAs were made optional in C11.
[HI] C99 में रनटाइम वेरिएबल से एरे आकार (जैसे int a[n];) तय किया जा सकता है, पर बड़ा n देने पर यह बिना किसी एरर के स्टैक ओवरफ्लो कर सकता है; इसलिए C11 में इन्हें वैकल्पिक बना दिया गया।
Post a Comment
Post a Comment