ProgramGuru

C Arrays, Indexes and Bounds

C Arrays, Indexes and Bounds

A report needs three subject marks in their original order. An array groups those same-type values into a numbered sequence.

An array stores a fixed number of same-type elements contiguously. A valid element index runs from zero through count minus one. C does not automatically check an index before access.

  1. Explain

    Visit every element once while preserving a valid index.

  2. Trace

    Replace the second mark with 38 before summing.

  3. Apply

    Use the element count to avoid an out-of-bounds index.

Arrays: follow the data

  1. Start

    A report needs three subject marks in their original order. An array groups those same-type values into a numbered sequence.

  2. Operation

    An array stores a fixed number of same-type elements contiguously. A valid element index runs from zero through count minus one. C does not automatically check an index before access.

  3. Result

    Total: 120

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

int marks[3] = {40, 35, 45} creates three elements. marks[0] is 40 and marks[2] is 45. The number three is an element count, not a valid final index.

sizeof marks / sizeof marks[0] obtains the count while marks is an actual array in this scope. A parameter written as int marks[] is adjusted to a pointer, so the same calculation inside that function does not recover the caller’s array count.

Read the complete C program

This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45};
  size_t count = sizeof marks / sizeof marks[0];
  int total = 0;
  for (size_t i = 0; i < count; i++) total += marks[i];
  printf("Total: %d\n", total);
  return 0;
}

Expected output

Total: 120
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45};
  size_t count = sizeof marks / sizeof marks[0];
  int total = 0;
  for (size_t i = 0; i < count; i++) total += marks[i];
  printf("Total: %d\n", total);
  return 0;
}

The count is three and the first index is zero.

The total changes from 0 to 40 to 75 to 120. i becomes three only for the stopping test, not for an element access.

Follow the worked calculation

  1. Read the starting values

    The count is three and the first index is zero.

  2. Perform the operation

    The total changes from 0 to 40 to 75 to 120. i becomes three only for the stopping test, not for an element access.

  3. Check the observable result

    Total: 120

    Total: 120
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45};
  size_t count = sizeof marks / sizeof marks[0];
  int total = 0;
  for (size_t i = 0; i < count; i++) total += marks[i];
  printf("Total: %d\n", total);
  return 0;
}

Replace the second mark with 38 before summing.

Worked answer

Index one identifies the second subject. The new sum is 40 + 38 + 45 = 123; the element count remains three.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45};
  marks[1] = 38;
  int total = 0;
  for (size_t i = 0; i < 3; i++) total += marks[i];
  printf("Total: %d\n", total);
  return 0;
}

Expected output

Total: 123
Run in C compiler →

Recognize the important boundaries

An out-of-bounds read is not a harmless extra value: it is undefined behavior. Validate externally supplied indexes before access.

An array itself cannot be assigned wholesale with = after declaration. Copy elements or use an appropriate byte-copy operation with a correct destination size.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Which index is the last valid one in int marks[3]?
Choose one answer. You can try again.

Answer and explanation

2

Correct. Indexes start at zero and stop before the element count.

Use the idea independently

An array stores a fixed number of same-type elements contiguously. A valid element index runs from zero through count minus one. C does not automatically check an index before access.

Continue with terminated character arrays.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Strings, Character Arrays and the Null Terminator →