Bird
0
0
DSA Cprogramming

Array Declaration and Initialization in DSA C

Choose your learning style9 modes available
Mental Model
An array is a collection of boxes lined up, each holding a value. Declaring and initializing means setting up these boxes and putting values inside them.
Analogy: Imagine a row of mailboxes, each labeled with a number. Declaring an array is like building the mailboxes, and initializing is like putting letters inside each mailbox.
Index: 0   1   2   3   4
Array: [ ] [ ] [ ] [ ] [ ]
Dry Run Walkthrough
Input: Declare an array of 5 integers and initialize it with values 10, 20, 30, 40, 50
Goal: Create an array with 5 boxes and fill each with the given numbers
Step 1: Declare an integer array of size 5
Index: 0   1   2   3   4
Array: [ ] [ ] [ ] [ ] [ ]
Why: We need to reserve space for 5 integers
Step 2: Initialize index 0 with 10
Index: 0   1   2   3   4
Array: [10] [ ] [ ] [ ] [ ]
Why: Put the first value in the first box
Step 3: Initialize index 1 with 20
Index: 0   1   2   3   4
Array: [10] [20] [ ] [ ] [ ]
Why: Put the second value in the second box
Step 4: Initialize index 2 with 30
Index: 0   1   2   3   4
Array: [10] [20] [30] [ ] [ ]
Why: Put the third value in the third box
Step 5: Initialize index 3 with 40
Index: 0   1   2   3   4
Array: [10] [20] [30] [40] [ ]
Why: Put the fourth value in the fourth box
Step 6: Initialize index 4 with 50
Index: 0   1   2   3   4
Array: [10] [20] [30] [40] [50]
Why: Put the fifth value in the fifth box
Result:
Index: 0   1   2   3   4
Array: [10] [20] [30] [40] [50]
Annotated Code
DSA C
#include <stdio.h>

int main() {
    // Declare and initialize array with 5 integers
    int arr[5] = {10, 20, 30, 40, 50};

    // Print array elements
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}
int arr[5] = {10, 20, 30, 40, 50};
Declare array of size 5 and initialize each index with given values
for (int i = 0; i < 5; i++) {
Loop through each index to access array elements
printf("%d ", arr[i]);
Print the value at current index
OutputSuccess
10 20 30 40 50
Complexity Analysis
Time: O(n) because printing all n elements requires visiting each once
Space: O(n) because array stores n elements in contiguous memory
vs Alternative: Compared to dynamic allocation, static array declaration is simpler and faster but fixed size
Edge Cases
Declare array with zero size
Compilation error or no storage allocated
DSA C
int arr[0];
Initialize fewer elements than size
Remaining elements are set to zero automatically
DSA C
int arr[5] = {10, 20};
Initialize more elements than size
Compilation error due to excess initializers
DSA C
int arr[5] = {10, 20, 30, 40, 50, 60};
When to Use This Pattern
When you need to store multiple values of the same type in order, use array declaration and initialization to set up fixed-size storage.
Common Mistakes
Mistake: Forgetting to specify array size when not initializing all elements
Fix: Always specify size or initialize all elements to let compiler infer size
Mistake: Accessing array index out of declared size
Fix: Ensure index is within 0 to size-1 to avoid undefined behavior
Summary
It creates a fixed-size collection of elements stored in order.
Use it when you know the number of elements in advance and want fast access by index.
The key is that declaration reserves space and initialization fills it with values.