Skip to content
Justice Tech School

Pass by Value and Pointers

C copies arguments. Pass a pointer to let a function change the caller variable.

Example
#include <stdio.h>

void swap(int *x, int *y) { int t = *x; *x = *y; *y = t; }
int main(void) {
  int a = 1, b = 2;
  swap(&a, &b);
  printf("%d %d\n", a, b);
  return 0;
}

Result

2 1

Exercise

Pointer parameter.

void f(int p)

Quiz Test

How do you modify a caller variable?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.