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
EasyWrite a C program that demonstrates passing pointers to functions. Your program should:
- Create a function named
displayValuethat takes a pointer to an integer as its parameter - Inside the
displayValuefunction, use the dereference operator to access the value at the memory address and print it in the format:Value at address: [value] - In the
mainfunction, declare an integer variable namednumberand initialize it with the value 42 - Print the original value in the format:
Original value: [value] - Call the
displayValuefunction and pass the address of thenumbervariable using the address-of operator - 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 completedThis 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;
}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