Bird
0
0
DSA Cprogramming

Array Access and Update at Index in DSA C

Choose your learning style9 modes available
Mental Model
An array is like a row of boxes where each box has a number. You can quickly find or change the number in any box by knowing its position.
Analogy: Imagine a row of mailboxes numbered from 0 upwards. To get or change the mail in mailbox number 3, you just go directly to mailbox 3 without checking others.
[0] 5 -> [1] 8 -> [2] 2 -> [3] 7 -> [4] 4
Dry Run Walkthrough
Input: array: [5, 8, 2, 7, 4], update index 2 with value 10
Goal: Change the value at position 2 to 10 and show the updated array
Step 1: Access the element at index 2 (which is 2)
[0] 5 -> [1] 8 -> [2] 2 -> [3] 7 -> [4] 4
Why: We need to find the exact box to update
Step 2: Update the element at index 2 to 10
[0] 5 -> [1] 8 -> [2] 10 -> [3] 7 -> [4] 4
Why: Replace old value with new value at the correct position
Result:
[0] 5 -> [1] 8 -> [2] 10 -> [3] 7 -> [4] 4
Annotated Code
DSA C
#include <stdio.h>

void updateAtIndex(int arr[], int size, int index, int value) {
    if (index < 0 || index >= size) {
        printf("Index out of bounds\n");
        return;
    }
    arr[index] = value; // update value at given index
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("[%d] %d", i, arr[i]);
        if (i != size - 1) printf(" -> ");
    }
    printf("\n");
}

int main() {
    int arr[] = {5, 8, 2, 7, 4};
    int size = sizeof(arr) / sizeof(arr[0]);
    int indexToUpdate = 2;
    int newValue = 10;

    printf("Original array:\n");
    printArray(arr, size);

    updateAtIndex(arr, size, indexToUpdate, newValue);

    printf("Updated array:\n");
    printArray(arr, size);

    return 0;
}
if (index < 0 || index >= size) {
check if index is valid to avoid errors
arr[index] = value; // update value at given index
directly replace the value at the specified index
OutputSuccess
Original array: [0] 5 -> [1] 8 -> [2] 2 -> [3] 7 -> [4] 4 Updated array: [0] 5 -> [1] 8 -> [2] 10 -> [3] 7 -> [4] 4
Complexity Analysis
Time: O(1) because accessing or updating an element by index in an array takes constant time
Space: O(1) because no extra space is needed besides the input array
vs Alternative: Compared to linked lists where access by position takes O(n), arrays allow instant access and update by index
Edge Cases
index is negative
function prints 'Index out of bounds' and does not update
DSA C
if (index < 0 || index >= size) {
index is equal or greater than array size
function prints 'Index out of bounds' and does not update
DSA C
if (index < 0 || index >= size) {
When to Use This Pattern
When you need to quickly find or change a value at a known position, use array access by index because it is direct and fast.
Common Mistakes
Mistake: Trying to access or update an index outside the array bounds causing runtime errors
Fix: Always check if the index is within 0 and size-1 before accessing or updating
Summary
It lets you get or change the value at a specific position in an array instantly.
Use it when you know the exact position of the element you want to access or update.
The key is that arrays let you jump directly to any position without checking others.