Pointers with Functions
0%
Lesson 1 of 2  ·  Pointers + Functions

Pointers with
Functions

10 very simple programs showing how pointers and functions work together. Each example teaches one clear idea — step by step from easiest to slightly harder.

1
Print via pointer
2
Change value
3
Swap two numbers
4
Double a number
5
Add 10 to value
6
Get min & max
7
Square in-place
8
Bigger number
9
Even or odd
10
Reset to zero
One rule to understand everything in this lesson:

Normally when you call a function, it gets a copy of your variable. The original stays untouched. If you want the function to actually change your variable, you pass its address using &. The function receives a pointer, uses * to reach in and change the real value.
❌ Pass by value — original unchanged
value.c
void addTen(int n) {
    n = n + 10;  /* copy only */
}
int x = 5;
addTen(x);
/* x is still 5 */
✓ Pass by pointer — original changes
pointer.c
void addTen(int *n) {
    *n = *n + 10;  /* real variable */
}
int x = 5;
addTen(&x);
/* x is now 15 */
example 1
1
Print a Value via a Pointer Parameter
Function receives a pointer and reads the value using *
The function takes a pointer int *p. It does NOT change the value — it just reads it using *p and prints it. This is the simplest possible pointer + function program.
ex1_print_pointer.c
C
#include <stdio.h>

void printValue(int *p) {
    printf("Value = %d\n", *p);  /* *p reads the value */
}

int main() {
    int x = 42;
    printValue(&x);  /* &x sends the address of x */

    int y = 99;
    printValue(&y);
    return 0;
}
output
Value = 42
Value = 99
Line by line
int *p
p is a pointer — it will hold an address, not a value
*p
go to the address p holds and read the value there — gives 42
&x
give me the address of x — this gets passed to the function
example 2
2
Change a Variable's Value From Inside a Function
Function modifies the original using *p = new value
The function receives the address of x, then writes a new value directly to that address using *p = 100. When the function returns, x in main has changed. This is the key power of pointers with functions.
ex2_change_value.c
C
#include <stdio.h>

void changeValue(int *p) {
    *p = 100;   /* writes 100 into the original variable */
}

int main() {
    int x = 5;
    printf("Before: x = %d\n", x);

    changeValue(&x);  /* pass address of x */

    printf("After : x = %d\n", x);
    return 0;
}
output
Before: x = 5
After : x = 100
*p = 100 does not create a new variable. It goes to the exact memory location where x lives and writes 100 there. So x itself becomes 100.
example 3
3
Swap Two Numbers
The classic pointer example — two pointers, temp variable
To swap x and y, the function needs to change both of them. So we pass both addresses&x and &y. Inside, we use a temp variable and three steps: save *a, put *b into *a, put saved value into *b.
ex3_swap.c
C
#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;  /* save a's value */
    *a = *b;        /* put b's value into a */
    *b = temp;      /* put saved value into b */
}

int main() {
    int x = 10, y = 25;
    printf("Before: x=%d  y=%d\n", x, y);
    swap(&x, &y);
    printf("After : x=%d  y=%d\n", x, y);
    return 0;
}
output
Before: x=10  y=25
After : x=25  y=10
The three swap steps — why temp is needed
temp = *a
save 10 into temp — if we don't, 10 is lost in the next step
*a = *b
write 25 into x — x is now 25
*b = temp
write saved 10 into y — y is now 10
example 4
4
Double a Number In-Place
Function multiplies the original variable by 2
The function reads the current value using *p, doubles it, and writes back using *p = *p * 2. The original variable is updated without returning anything. Called on the same variable twice to show it keeps doubling.
ex4_double.c
C
#include <stdio.h>

void doubleIt(int *p) {
    *p = *p * 2;   /* read current value, double it, write back */
}

int main() {
    int num = 5;
    printf("Start  : %d\n", num);
    doubleIt(&num);
    printf("x2     : %d\n", num);
    doubleIt(&num);
    printf("x2 x2  : %d\n", num);
    doubleIt(&num);
    printf("x2 x2 x2: %d\n", num);
    return 0;
}
output
Start    : 5
x2       : 10
x2 x2    : 20
x2 x2 x2 : 40
example 5
5
Add 10 to Any Variable
void function updates the original using *p += 10
*p += 10 is shorthand for *p = *p + 10. The function adds 10 directly to whatever variable's address it receives. Called on three different variables to show it works on any of them.
ex5_add10.c
C
#include <stdio.h>

void addTen(int *p) {
    *p += 10;   /* same as *p = *p + 10 */
}

int main() {
    int a = 5, b = 20, c = 100;

    addTen(&a);
    addTen(&b);
    addTen(&c);

    printf("a = %d\n", a);  /* 15  */
    printf("b = %d\n", b);  /* 30  */
    printf("c = %d\n", c);  /* 110 */
    return 0;
}
output
a = 15
b = 30
c = 110
example 6
6
Get Minimum and Maximum — Two Output Pointers
One function fills two results through two pointer parameters
C functions can only return one value. To get two results back, pass two output pointers. The function writes the min into *mn and the max into *mx. After calling, both variables in main have the answers.
ex6_minmax.c
C
#include <stdio.h>

/* Writes min into *mn and max into *mx */
void minMax(int a, int b, int *mn, int *mx) {
    if (a < b) { *mn = a; *mx = b; }
    else        { *mn = b; *mx = a; }
}

int main() {
    int small, large;
    minMax(7, 3, &small, &large);
    printf("Min = %d\n", small);
    printf("Max = %d\n", large);

    minMax(50, 80, &small, &large);
    printf("Min = %d\n", small);
    printf("Max = %d\n", large);
    return 0;
}
output
Min = 3
Max = 7
Min = 50
Max = 80
This is how scanf works. scanf("%d", &x) — scanf takes the address of x and writes the typed value directly into it using a pointer. That is exactly what *mn = a does here.
example 7
7
Square a Number In-Place
Replace the variable's value with its square
ex7_square.c
C
#include <stdio.h>

void squareIt(int *p) {
    *p = (*p) * (*p);  /* square and write back */
}

int main() {
    int a = 4, b = 7, c = 9;
    squareIt(&a); squareIt(&b); squareIt(&c);
    printf("4² = %d\n", a);
    printf("7² = %d\n", b);
    printf("9² = %d\n", c);
    return 0;
}
output
4² = 16
7² = 49
9² = 81
Why the brackets around *p? (*p) * (*p) — the brackets make sure C dereferences p first, then multiplies. Without them, *p * *p still works but is harder to read clearly.
example 8
8
Find the Bigger of Two — Return a Pointer
Function returns int* pointing to the larger variable
A function can return a pointer. Here it compares two values and returns the address of whichever is bigger. The caller dereferences the returned pointer to get the bigger value.
ex8_bigger.c
C
#include <stdio.h>

/* Returns pointer to the bigger variable */
int *bigger(int *a, int *b) {
    if (*a > *b)
        return a;   /* return address of a */
    else
        return b;   /* return address of b */
}

int main() {
    int x = 30, y = 75;
    int *result = bigger(&x, &y);
    printf("Bigger = %d\n", *result);

    int p = 99, q = 50;
    printf("Bigger = %d\n", *bigger(&p, &q));
    return 0;
}
output
Bigger = 75
Bigger = 99
example 9
9
Check Even or Odd via Pointer
Function reads value through pointer and checks %2
ex9_evenodd.c
C
#include <stdio.h>

void checkEvenOdd(int *p) {
    if (*p % 2 == 0)
        printf("%d is EVEN\n", *p);
    else
        printf("%d is ODD\n", *p);
}

int main() {
    int a = 4, b = 7, c = 10, d = 13;
    checkEvenOdd(&a);
    checkEvenOdd(&b);
    checkEvenOdd(&c);
    checkEvenOdd(&d);
    return 0;
}
output
4 is EVEN
7 is ODD
10 is EVEN
13 is ODD
example 10
10
Reset Any Variable to Zero
Simplest pointer function — *p = 0
The simplest possible in-place modification — set a variable to zero through its pointer. One line inside the function. Shows that any variable can be reset by passing its address.
ex10_reset.c
C
#include <stdio.h>

void resetToZero(int *p) {
    *p = 0;
}

int main() {
    int score = 100, lives = 3, coins = 500;
    printf("Before: score=%d  lives=%d  coins=%d\n",
           score, lives, coins);

    resetToZero(&score);
    resetToZero(&lives);
    resetToZero(&coins);

    printf("After : score=%d  lives=%d  coins=%d\n",
           score, lives, coins);
    return 0;
}
output
Before: score=100  lives=3  coins=500
After : score=0    lives=0  coins=0
checklist
  • To let a function change your variable — pass its address using & and receive it as int *p
  • *p reads the value at the address p holds
  • *p = 100 writes 100 directly into the original variable
  • Swap needs temp = *a, *a = *b, *b = temp — three steps, not two
  • Two output pointers let one function fill two results — like minMax(a, b, &mn, &mx)
  • A function can return int* — a pointer to a variable
  • scanf uses &x for the same reason — it writes the typed value into your variable via pointer