Menu
Coddy logo textTech

Passing Pointers to Functions

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

Now that you understand pass-by-value's limitations, let's explore how passing pointers to functions solves this problem. Instead of passing a copy of a variable's value, you can pass the variable's memory address to the function.

When you pass a pointer to a function, the function receives access to the original variable's location in memory. This means the function can work directly with the original data, not just a copy of it.

void processValue(int *ptr) {
    // ptr holds the address of the original variable
    // The function can now access that memory location
}

int main() {
    int x = 10;
    processValue(&x);  // Pass the address of x
    return 0;
}

Notice how we use the address-of operator & when calling the function to pass the variable's address. The function parameter is declared as int *ptr, indicating it expects to receive a pointer to an integer. This fundamental technique opens up powerful possibilities for functions to interact with your program's data.

challenge icon

Challenge

Easy

Write a C program that demonstrates passing pointers to functions. Your program should:

  1. Create a function named displayValue that takes a pointer to an integer as its parameter
  2. Inside the displayValue function, use the dereference operator to access the value at the memory address and print it in the format: Value at address: [value]
  3. In the main function, declare an integer variable named number and initialize it with the value 42
  4. Print the original value in the format: Original value: [value]
  5. Call the displayValue function and pass the address of the number variable using the address-of operator
  6. After the function call, print a confirmation message: Function call completed

Your output should demonstrate that the function successfully receives and accesses the memory address of the original variable:

Original value: 42
Value at address: 42
Function call completed

This challenge introduces the fundamental concept of passing pointers to functions, allowing the function to access the original variable's memory location rather than working with a copy of its value.

Cheat sheet

When passing pointers to functions, the function receives access to the original variable's memory location instead of a copy of its value.

Use the address-of operator & to pass a variable's address to a function:

void processValue(int *ptr) {
    // ptr holds the address of the original variable
    // The function can now access that memory location
}

int main() {
    int x = 10;
    processValue(&x);  // Pass the address of x
    return 0;
}

The function parameter is declared as int *ptr to indicate it expects to receive a pointer to an integer.

Try it yourself

#include <stdio.h>

// TODO: Write your displayValue function here

int main() {
    // TODO: Write your code here
    // 1. Declare and initialize the number variable
    // 2. Print the original value
    // 3. Call the displayValue function
    // 4. Print the completion message
    
    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