C Pass by Reference

Beginner
8 min read
Updated: Nov 2026
2 examples

What Is Pass by Reference?

To let a function change a caller's variable, pass its address with & and receive it as a pointer parameter. Inside the function, *p reads or writes the original variable. This is how scanf stores values and how swap works.

Remember
void swap(int *a, int *b) {
    int t = *a;  *a = *b;  *b = t;
}

swap(&x, &y);   /* pass addresses */

Cheat Sheet

Keep these facts handy while you work with Pass by Reference in C.

TopicDetail
Callerf(&var)
Parametertype *p
Use*p = value; writes to the original
Read-onlyconst type *p promises not to write

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Arrays As Parameters.

Example 1 — Swap two integers

Both values live in main; swap edits them through pointers.

C
#include <stdio.h>

void swap(int *a, int *b) {
    int t = *a;
    *a = *b;
    *b = t;
}

int main(void) {
    int x = 3, y = 9;
    printf("before: x=%d y=%d\n", x, y);
    swap(&x, &y);
    printf("after:  x=%d y=%d\n", x, y);
    return 0;
}

How It Works

  1. &x and &y pass the addresses.
  2. *a and *b name the original variables.
  3. The three assignments swap the stored values.

Example 2 — Return two results through pointers

One call computes both the minimum and maximum.

C
#include <stdio.h>

void min_max(const int *arr, int n, int *min, int *max) {
    *min = *max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] < *min) *min = arr[i];
        if (arr[i] > *max) *max = arr[i];
    }
}

int main(void) {
    int data[] = {4, 9, 1, 7};
    int lo, hi;
    min_max(data, 4, &lo, &hi);
    printf("min=%d max=%d\n", lo, hi);
    return 0;
}

How It Works

  1. lo and hi are output parameters filled by the function.
  2. const int *arr promises the array will not be modified.
  3. This is the C way to return several values without a struct.

Edge Cases & Pitfalls

Check these before relying on Pass by Reference in real programs.

null

NULL pointer arguments

Writing through a NULL pointer crashes. Check pointer parameters when callers may pass NULL.

alias

Aliasing the same variable

swap(&x, &x) with the XOR trick zeroes the variable. The temp-variable version is safe.

Key Takeaways

  • Address: pass &var.
  • Pointer: receive type *p.
  • Deref: use *p to change the original.

One line: To let a function change a caller's variable, pass its address with & and receive it as a pointer parameter.

Frequently Asked Questions

Pass the variable's address (&var) and declare the parameter as a pointer. Inside the function use *param to access the caller's variable.
By value copies the data. Passing a pointer copies only the address, so the function can read and modify the original object.
Continue with "C Arrays As Parameters" next. Arrays decay to pointers, so pass the length too. The C Functions hub lists every topic in order.

Next: Arrays As Parameters

Arrays decay to pointers, so pass the length too.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful