← Back to C Course | Chapter 7: Pointers | Lesson 5 of 9

C Pointers & Functions

Passing pointers to functions lets them change the original data, like handing over an address instead of a copy. It is how a function can swap two of your variables.
Syntax
c
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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting a function to change the caller's variable when it received a copy by value, as in a swap that swaps only copies.
  2. Forgetting the & at the call site when the function expects a pointer, so the wrong value is passed.
  3. Returning a pointer to a local variable, which is destroyed when the function returns.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.