Bird
Raised Fist0
DSA Cprogramming

Array Access and Update at Index in DSA C

Choose your learning style10 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.

Practice

(1/5)
1. What does the expression arr[2] do in an array named arr?
easy
A. Accesses the element at index 0 of the array
B. Accesses the element at index 2 of the array
C. Updates the element at index 2 to zero
D. Deletes the element at index 2

Solution

  1. Step 1: Understand array indexing

    Arrays use zero-based indexing, so index 2 means the third element.
  2. Step 2: Access element at index 2

    The expression arr[2] retrieves the value stored at the third position in the array.
  3. Final Answer:

    Accesses the element at index 2 of the array -> Option B
  4. Quick Check:

    arr[2] means third element access [OK]
Hint: Remember arrays start at index 0, so arr[2] is third element [OK]
Common Mistakes:
  • Thinking index starts at 1 instead of 0
  • Confusing access with update or delete
  • Assuming arr[2] changes the array
2. Which of the following is the correct syntax to update the 4th element of an integer array arr to 10 in C?
easy
A. arr[3] = 10;
B. arr(4) = 10;
C. arr[4] == 10;
D. arr{3} = 10;

Solution

  1. Step 1: Identify correct array indexing syntax

    In C, arrays use square brackets with zero-based indexing, so the 4th element is at index 3.
  2. Step 2: Use assignment operator correctly

    To update, use single equals =, so arr[3] = 10; is correct.
  3. Final Answer:

    arr[3] = 10; -> Option A
  4. Quick Check:

    Use arr[index] = value; with zero-based index [OK]
Hint: Use square brackets and single = for update, zero-based index [OK]
Common Mistakes:
  • Using parentheses or braces instead of brackets
  • Using double equals (==) instead of assignment (=)
  • Using wrong index number for 4th element
3. What will be the output of the following C code?
int arr[5] = {1, 2, 3, 4, 5};
arr[1] = arr[3] + 2;
printf("%d", arr[1]);
medium
A. 6
B. 4
C. 5
D. 7

Solution

  1. Step 1: Evaluate the expression arr[3] + 2

    arr[3] is the 4th element, which is 4. Adding 2 gives 6.
  2. Step 2: Assign and print arr[1]

    arr[1] is updated to 6, then printed, so output is 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    arr[3]=4 + 2 = 6 printed [OK]
Hint: Remember arr[3] is 4, add 2, assign to arr[1], print arr[1] [OK]
Common Mistakes:
  • Confusing index 1 with index 3 values
  • Adding incorrectly or forgetting zero-based index
  • Printing wrong array element
4. Identify the error in the following C code snippet:
int arr[3] = {5, 10, 15};
arr[3] = 20;
printf("%d", arr[3]);
medium
A. Syntax error in array declaration
B. Missing semicolon after assignment
C. Array index out of bounds error
D. Incorrect printf format specifier

Solution

  1. Step 1: Check array size and valid indices

    Array size is 3, valid indices are 0, 1, 2. Index 3 is out of bounds.
  2. Step 2: Understand consequences of out of bounds access

    Accessing or assigning arr[3] causes undefined behavior or runtime error.
  3. Final Answer:

    Array index out of bounds error -> Option C
  4. Quick Check:

    Index 3 invalid for size 3 array [OK]
Hint: Array indices go from 0 to size-1 only [OK]
Common Mistakes:
  • Using index equal to array size
  • Ignoring zero-based indexing
  • Assuming no error for out of bounds
5. Given an integer array arr of size 6 initialized as {2, 4, 6, 8, 10, 12}, which code snippet correctly doubles the value at index 4 and then prints the entire array?
hard
A. arr[4] = arr[4] + 2; for(int i=0; i<5; i++) printf("%d ", arr[i]);
B. arr[5] = arr[4] * 2; for(int i=0; i<=6; i++) printf("%d ", arr[i]);
C. arr[4] *= 2; for(int i=1; i<6; i++) printf("%d ", arr[i]);
D. arr[4] = arr[4] * 2; for(int i=0; i<6; i++) printf("%d ", arr[i]);

Solution

  1. Step 1: Correctly update value at index 4

    Index 4 is the 5th element. Doubling means multiply by 2. Both arr[4] = arr[4] * 2; and arr[4] *= 2; work, but check loop next.
  2. Step 2: Print entire array correctly

    Array size is 6, so loop must run from 0 to 5 (i<6). arr[4] = arr[4] * 2; for(int i=0; i<6; i++) printf("%d ", arr[i]); uses correct loop range and prints all elements.
  3. Final Answer:

    arr[4] = arr[4] * 2; for(int i=0; i<6; i++) printf("%d ", arr[i]); -> Option D
  4. Quick Check:

    Update index 4, loop 0 to 5 to print all [OK]
Hint: Use zero-based index and loop from 0 to size-1 [OK]
Common Mistakes:
  • Using wrong index for update
  • Looping beyond array size causing errors
  • Printing partial array missing elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard