Share:

Arrays

advanced

Part of Advanced C

Theory

Arrays are contiguous memory locations that store multiple values of the same type. They provide efficient indexed access to data.

One-dimensional arrays are declared with a fixed size:

int numbers[5];          // declaration
int scores[5] = {90, 85, 78, 92, 88};  // initialization
int values[] = {1, 2, 3};  // size inferred

Array indices start at 0. The last element of an array of size n is at index n-1.

int arr[3] = {10, 20, 30};
printf("%d", arr[0]);  // 10
arr[2] = 35;           // modify last element

Array traversal uses loops:

for (int i = 0; i < 5; i++) {
    printf("%d ", numbers[i]);
}

Multi-dimensional arrays have rows and columns:

int matrix[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};
printf("%d", matrix[1][2]);  // 7

Strings in C are character arrays terminated by a null character '\0'. The null terminator marks the end of the string.

char name[] = "Alice";       // {'A','l','i','c','e','\0'} — size 6
char hello[6] = "Hello";     // 5 chars + '\0'

The <string.h> library provides string manipulation functions:

  • strlen(s) — length (excluding null terminator)
  • strcpy(dest, src) — copy string
  • strcat(dest, src) — concatenate
  • strcmp(s1, s2) — compare (returns 0 if equal)
  • strstr(haystack, needle) — find substring

Passing arrays to functions: Arrays are passed by reference (the array name is a pointer to the first element). The size information is lost, so you must pass the size separately.

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
}

Practical Examples

Example 1: One-Dimensional Arrays and Traversal
c
Example 2: Strings and string.h Functions
c

Exercises

Reverse an Array

easy

Write a C program that declares an array of 5 integers, initializes them, and prints them in reverse order. Then reverse the array in-place using a loop.

Starter Code:

#include <stdio.h>\n\nint main() {\n  int arr[] = {10, 20, 30, 40, 50};\n  int size = 5;\n  // Print in reverse\n  // Reverse in place\n  return 0;\n}

Expected Output:

Original: 10 20 30 40 50\nReversed: 50 40 30 20 10

2D Array — Matrix Operations

medium

Create a 3x3 matrix and write functions to: 1) Print the matrix, 2) Calculate the sum of all elements, 3) Find the sum of the main diagonal. Use nested loops.

Starter Code:

#include <stdio.h>\n\nvoid printMatrix(int matrix[][3], int rows) {\n  // Your code here\n}\n\nint sumMatrix(int matrix[][3], int rows) {\n  // Your code here\n}\n\nint sumDiagonal(int matrix[][3], int rows) {\n  // Your code here\n}

Expected Output:

Matrix:\n1 2 3\n4 5 6\n7 8 9\nSum: 45\nDiagonal sum: 15

String Palindrome Checker

medium

Write a program that reads a string from the user and checks if it's a palindrome (reads the same forwards and backwards). Use strlen() and character comparison.

Expected Output:

Enter a word: racecar\nracecar is a palindrome!

Mini Quiz

Mini Quiz

Mini Project

Mini Project: Student Grade Analyzer

Create a C program that stores student grades in arrays and performs analysis — calculate averages, find highest/lowest, and sort the grades.

Requirements:

    Bonus Challenge

    Implement a linear search function that finds a student by name and displays their grade.