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
EasyWrite a C program that demonstrates modifying variables through pointers by creating a function that triples the value of an integer.
Your program should:
- Create a function named
tripleValuethat takes a pointer to an integer as its parameter - Inside the
tripleValuefunction, use the dereference operator to multiply the value at the memory address by 3 - Inside the
tripleValuefunction, print the new value in the format:Value tripled to: [value] - In the
mainfunction, declare an integer variable namednumberand initialize it with the value 8 - Print the original value in the format:
Original value: [value] - Call the
tripleValuefunction and pass the address of thenumbervariable using the address-of operator - After the function call, print the final value of
numberin 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: 24This 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;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions