C - Arrays

Overview

An array stores a fixed-size sequence of elements of the same type in contiguous memory.

Learning Objectives

  • Declare, initialize, and index arrays.
  • Understand array bounds and decay to pointer.

Prerequisites

Example

#include <stdio.h>

int main(void) {
  int a[3] = {10,20,30};
  for (int i = 0; i < 3; i++) printf("%d ", a[i]);
  printf("\n");
}

Common Pitfalls

  • Indexing out of bounds is undefined behavior.
  • Arrays are not resizable; use dynamic allocation if size varies.

Checks for Understanding

  1. What is the type of a when passed to a function?
Show answer

It decays to int* (pointer to first element).

Expected Output

10 20 30 

Use Cases

  • Buffers for I/O and networking.
  • Lookup tables for algorithms.
  • Fixed-size collections for embedded systems.

Exercises

  1. Initialize an array of 5 integers to the first five squares and print them.
  2. Find the minimum and maximum of an array in a single pass.