Array Basics in C: Indexing, Memory Layout and Worked Exam Traces

C arrays become much easier once indices, addresses, loop bounds, and mutations are kept separate. Build that model with two complete traces and common-error checks.

KnowledgeGate Team

Exam prep & CS education

Updated 16 Sep 20266 min read

Array questions can look like simple syntax checks, but they often combine zero-based indexing, current values after mutation, loop bounds, and contiguous storage. A reliable model helps you write a correct traversal and trace exam code without guessing. One-dimensional C arrays involve declaration, initialization, address calculation, traversal, mutation, traps, and output prediction. For the wider syllabus, use GATE CS Exam Preparation Courses & Test Series.

Array basics in C: one type, fixed length, contiguous cells

An array is a fixed-length sequence of elements of one type, stored in contiguous memory cells. In int scores[6];, the element type is int, the length is 6, and the legal indices are 0 through 5. scores[6] is outside this array.

Keep three ideas separate. scores is the array object, scores[2] is one element, and &scores[2] is that element's address. The array's length stays fixed after declaration, but an assignment can change any element's value.

The anchor array is:

c
int a[6] = {14, 7, 21, 4, 18, 9};

Start with these six values. The later update changes only index 2.

Array declaration and initialization: read each form exactly

These declarations look similar but establish different values and lengths:

c
int a[6] = {14, 7, 21, 4, 18, 9};
int b[6] = {14, 7};
int c[] = {3, 6, 9, 12};

a contains all six listed values. Partial aggregate initialization makes b equal to {14, 7, 0, 0, 0, 0}. With no written length, the compiler infers four elements for c.

For this example, assume an int occupies 4 bytes. Then sizeof(c) = 4 x 4 = 16, sizeof(c[0]) = 4, and sizeof(c) / sizeof(c[0]) = 16 / 4 = 4. This length calculation works where c is still an array. A function parameter written as int c[] is adjusted to a pointer, so the same expression there does not recover the caller's array length.

Also distinguish int local[6]; from partial initialization. The elements of an uninitialized automatic local array have indeterminate values until assigned. They are not dependable zeros.

Array indexing and memory layout: calculate the address

For this illustration only, assume &a[0] has byte address 1000 and sizeof(int) is 4. The address formula is:

address of a[i] = 1000 + i x 4

It gives &a[0] = 1000, &a[1] = 1004, &a[2] = 1008, &a[3] = 1012, &a[4] = 1016, and &a[5] = 1020. Therefore, a[4] stores the value 18 at the illustrated address 1016. Real addresses and the size of int are implementation-dependent, so use the values stated in a problem.

In an element expression, a[i] is equivalent to *(a + i). The array name often converts to a pointer to its first element, which makes that arithmetic work. The array itself is not a modifiable pointer, however, so a++ is invalid.

After the one-dimensional index model is secure, Relationship Between Arrays and Pointers in C: Decay, Address Arithmetic and a Worked Memory Trace develops array decay and the different pointer steps made by a + 1 and &a + 1. Arrays and Pointers in C: Complete Guide with Worked Examples for GATE and Interviews extends that layer to pointer precedence and two-dimensional address arithmetic. Those topics go beyond the fixed-element formula used for array basics.

Memory map of int a[6] = {14, 7, 21, 4, 18, 9} with indices 0 to 5 at byte addresses 1000 to 1020.

Array traversal worked example: sum, minimum and update

Traverse all six cells with i starting at 0 and continuing while i < 6:

c
int sum = 0;
int minimum = a[0];
int minimum_index = 0;

for (int i = 0; i < 6; i++) {
    sum += a[i];
    if (a[i] < minimum) {
        minimum = a[i];
        minimum_index = i;
    }
}

i

Value read

Running sum

Running minimum

0

14

14

14

1

7

21

7

2

21

42

7

3

4

46

4

4

18

64

4

5

9

73

4

The result is sum = 73, minimum = 4, and minimum_index = 3. For the sum, the loop invariant is simple: before iteration i, sum describes exactly the elements at indices 0 through i - 1. After the iteration, the tracked results describe indices 0 through i. Thus i < 6 covers every cell, while i <= 6 attempts an invalid seventh access.

Now update one element: a[2] = a[0] + a[5] = 14 + 9 = 23. The array becomes {14, 7, 23, 4, 18, 9}. Its revised sum is 73 - 21 + 23 = 75. Any later read of index 2 must use 23, not the original 21.

Traversal trace of int a[6] showing running sum and minimum, then a[2] updated to 23 for a final sum of 75.

Array output trace: follow mutations in execution order

Consider this complete trace:

c
int x[] = {2, 5, 8, 11, 14};

for (int i = 1; i < 5; i += 2) {
    x[i] = x[i] + x[i - 1];
}

printf("%d %d %d", x[1], x[3], x[4]);

At i = 1, the assignment is x[1] = 5 + 2 = 7. The array immediately becomes {2, 7, 8, 11, 14}. At i = 3, it is x[3] = 11 + 8 = 19, giving {2, 7, 8, 19, 14}. The program prints 7 19 14.

Use the same tracing method every time: draw an index row and a value row, record each write immediately, and let every later read see the current row. Applying all assignments mentally to the original initializer can produce a wrong answer when one iteration reads a value changed by an earlier iteration.

Array traps in C: bounds, decay and misleading shortcuts

Correct these common mistakes at the model level:

  • a[6] on a six-element array is out of bounds, so accessing it has undefined behaviour. for (i = 0; i <= 5; i++) stays within indices 0 through 5, but i <= 6 does not.

  • An uninitialized automatic local element does not provide a dependable zero. Assign a value before reading it.

  • sizeof(a) / sizeof(a[0]) gives 6 while a is the six-element array in this scope. Inside void f(int a[]), the parameter is a pointer, so that expression measures pointer-related sizes instead. Pass the length explicitly, as in f(a, 6).

  • Whole arrays cannot be assigned with a = b. Copy individual elements with a loop when working with this basic model.

  • An array name cannot be incremented with a++. It is not a pointer variable that can be redirected.

These are not harmless style differences. An invalid bound can make the program's behaviour undefined, while an incorrect sizeof shortcut can silently give the wrong length.

Array exam patterns: recognise what the question is testing

Most basic array questions ask you to do one of five things: infer values after partial initialization, calculate an address from a stated base and element size, trace a loop with mutations, identify an out-of-bounds access, or relate a[i] to *(a + i). The past-cycle GATE 2026 Information Brochure lists Programming in C and arrays in the CS syllabus. That syllabus-level fact says nothing about marks, frequency, weightage, or a future cycle.

Use a 30-second protocol: write the legal index range, copy the starting values into cells, annotate every write in execution order, and stop with "undefined behaviour" if evaluation requires an invalid access. Do not invent an output. Apply the method across over 30 Array Basics questions in the practice bank, then repeat both worked traces from a blank index-and-value table.

Array basics: the short version and next step

Keep five points: indices run from 0 to n - 1; elements occupy contiguous cells; address calculations use the element size stated in the problem; loop bounds control safety; and every mutation changes later reads. To learn the language in sequence, use the C Language Course: Concepts, MCQs & Coding. For revision, rework both traces without looking back, then explain why the updated sum is 75 and why the output is 7 19 14. If both explanations follow from the current cells, your array model is ready for the next topic.