Menu
Coddy logo textTech

Modifying Vars via Pointers

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

Now you can harness the true power of pointers: modifying the original variable through a function. When you pass a pointer to a function, the function can use the dereference operator to change the value stored at that memory address.

Inside the function, you dereference the pointer using the * operator to access and modify the original variable's value. This technique is called pass-by-reference because you're passing a reference to the variable's location in memory.

void doubleValue(int *ptr) {
    *ptr = *ptr * 2;  // Dereference to modify the original value
}

int main() {
    int x = 5;
    doubleValue(&x);  // Pass the address of x
    // x is now 10!
    return 0;
}

The key is using *ptr on the left side of the assignment to modify the value at the memory address the pointer holds. This allows functions to permanently change variables from the calling function, opening up powerful programming possibilities that pass-by-value simply cannot achieve.

challenge icon

Challenge

Easy

Write a C program that demonstrates modifying variables through pointers by creating a function that triples the value of an integer.

Your program should:

  1. Create a function named tripleValue that takes a pointer to an integer as its parameter
  2. Inside the tripleValue function, use the dereference operator to multiply the value at the memory address by 3
  3. Inside the tripleValue function, print the new value in the format: Value tripled to: [value]
  4. In the main function, declare an integer variable named number and initialize it with the value 8
  5. Print the original value in the format: Original value: [value]
  6. Call the tripleValue function and pass the address of the number variable using the address-of operator
  7. After the function call, print the final value of number in the format: Final value: [value]

Your output should demonstrate that the function successfully modifies the original variable through the pointer:

Original value: 8
Value tripled to: 24
Final value: 24

This challenge demonstrates the power of pass-by-reference using pointers, where the function can permanently modify the original variable by dereferencing the pointer and changing the value at that memory location.

Cheat sheet

You can modify the original variable through a function using pointers. This technique is called pass-by-reference because you're passing a reference to the variable's location in memory.

Inside the function, dereference the pointer using the * operator to access and modify the original variable's value:

void doubleValue(int *ptr) {
    *ptr = *ptr * 2;  // Dereference to modify the original value
}

int main() {
    int x = 5;
    doubleValue(&x);  // Pass the address of x
    // x is now 10!
    return 0;
}

Use *ptr on the left side of the assignment to modify the value at the memory address the pointer holds. This allows functions to permanently change variables from the calling function.

Try it yourself

#include <stdio.h>

// TODO: Write your tripleValue function here

int main() {
    // TODO: Write your code here
    // Declare and initialize the number variable
    // Print the original value
    // Call the tripleValue function
    // Print the final value
    
    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