C Pointers & Functions
In this page:
void function_name(data_type param) { } // call by value
void function_name(data_type *param) { // call by reference
*param = new_value;
}
function_name(&variable_name);
Call by Value
This is C's default parameter-passing behavior for all non-array, non-pointer types -- it protects the caller's data but means the function has no way to report a result back except through its return value, which limits it to a single output.
Example: Call by Value
// Include standard input/output (printf, scanf)
#include <stdio.h>
void tryChange(int x) {
x = 100;
}
// Program execution starts in main()
int main() {
// Declare value and set it to 5
int value = 5;
tryChange(value);
// Print formatted text to the screen
printf("%d", value);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Call by Reference
This is the standard technique C uses to simulate 'output parameters,' since a function can only truly return one value -- functions like scanf() rely entirely on this pattern, which is why you write scanf("%d", &x) with the address-of operator.
Example: Call by Reference
// Include standard input/output (printf, scanf)
#include <stdio.h>
void change(int *x) {
*x = 100;
}
// Program execution starts in main()
int main() {
// Declare value and set it to 5
int value = 5;
change(&value);
// Print formatted text to the screen
printf("%d", value);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Swapping Two Numbers
A classic teaching example that demonstrates why call by value alone can't swap two variables: the function must receive pointers to both variables so it can dereference and exchange their actual stored values, not just local copies.
Example: Swapping Two Numbers
// Include standard input/output (printf, scanf)
#include <stdio.h>
void swap(int *a, int *b) {
// Declare temp and set it to *a
int temp = *a;
*a = *b;
*b = temp;
}
// Program execution starts in main()
int main() {
int x = 5, y = 10;
swap(&x, &y);
// Print formatted text to the screen
printf("%d %d", x, y);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning Pointers from Functions
This is a genuinely dangerous pattern to avoid -- a local variable's memory is reused as soon as the function returns, so a pointer to it becomes a 'dangling pointer' pointing at invalid, potentially overwritten memory.
Example: Returning Pointers from Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
int* getStatic() {
static int value = 42;
// Send &value back to the caller
return &value;
}
// Program execution starts in main()
int main() {
int *ptr = getStatic();
// Print formatted text to the screen
printf("%d", *ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Passing Pointer Arrays
Passing an array of pointers (like char *names[]) lets a single function operate on a whole collection of separately-allocated strings or structures without needing to know their individual sizes in advance -- this is exactly how argv works in main().
Example: Passing Pointer Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printNames(char *names[], int count) {
// Loop: repeat while the condition holds
for (int i = 0; i < count; i++) {
// Print formatted text to the screen
printf("%s ", names[i]);
}
}
// Program execution starts in main()
int main() {
char *names[2] = {"Ann", "Bob"};
printNames(names, 2);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Expecting a function to change the caller's variable when it received a copy by value, as in a swap that swaps only copies.
- Forgetting the
&at the call site when the function expects a pointer, so the wrong value is passed. - Returning a pointer to a local variable, which is destroyed when the function returns.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: