Menu
Coddy logo textTech

Passing Arrays to Functions

Part of the Logic & Flow section of Coddy's C journey — lesson 29 of 63.

When you pass an array to a function in C, something interesting happens behind the scenes. The array itself isn't actually copied to the function. Instead, what gets passed is a pointer to the first element of the array.

This means that when you write a function parameter like int arr[] or int *arr, both are equivalent - they both represent a pointer to the first element of the array. The function receives the memory address where the array begins, giving it direct access to the original array's data.

void printArray(int arr[], int size) {
    // arr is actually a pointer to the first element
    // The function can access and modify the original array
}

Since the function works with the original array's memory location rather than a copy, any changes made inside the function will affect the original array. This is different from passing individual variables, which creates copies.

You'll also notice that you need to pass the array's size as a separate parameter because the function can't determine the array's length from the pointer alone.

challenge icon

Challenge

Easy

Write a C program that demonstrates passing arrays to functions by creating a function that processes temperature data. Your program should:

  1. Create a function named analyzeTemperatures that takes an integer array and its size as parameters
  2. Inside the analyzeTemperatures function, calculate and print the average temperature with exactly 1 decimal place in the format: Average temperature: [value]
  3. Inside the analyzeTemperatures function, find and print the highest temperature in the format: Highest temperature: [value]
  4. Inside the analyzeTemperatures function, count how many temperatures are above 25 degrees and print the count in the format: Days above 25 degrees: [count]
  5. In the main function, read an integer n representing the number of temperature readings
  6. Read n temperature values and store them in an integer array
  7. Call the analyzeTemperatures function, passing the array and its size

Your program should process the temperature data and display the analysis results. For example, if the input is:

5
20 28 15 32 26

Your output should be:

Average temperature: 24.2
Highest temperature: 32
Days above 25 degrees: 3

This challenge demonstrates how arrays are passed to functions as pointers to their first element, allowing the function to access and process all elements of the original array without creating a copy.

Cheat sheet

When passing arrays to functions in C, the array itself isn't copied. Instead, a pointer to the first element is passed.

Both int arr[] and int *arr are equivalent function parameters - they represent a pointer to the first element:

void printArray(int arr[], int size) {
    // arr is actually a pointer to the first element
    // The function can access and modify the original array
}

Key points:

  • Functions receive the memory address where the array begins
  • Changes made inside the function affect the original array
  • Array size must be passed as a separate parameter since the function can't determine length from the pointer alone

Try it yourself

#include <stdio.h>

// TODO: Create the analyzeTemperatures function here

int main() {
    // Read number of temperature readings
    int n;
    scanf("%d", &n);
    
    // Declare array to store temperatures
    int temperatures[n];
    
    // Read temperature values
    for (int i = 0; i < n; i++) {
        scanf("%d", &temperatures[i]);
    }
    
    // TODO: Call the analyzeTemperatures function here
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow