Technically, C does not have pass by reference. Everything is pass by value. To allow a function to change the value of a parameter passed in, and have that change remain after the function exits, you have to pass a pointer to the thing. For example:
Code:
#include <stdio.h>
void foo(int x)
{
x = 17;
printf("In foo(), x = %d\n", x);
}
void bar(int *x)
{
*x = 17; // x is a pointer, *x is the number stored at that address
printf("In bar(), x = %d\n", *x);
}
int main(void)
{
int x = 42;
printf("In main, x = %d\n", x);
foo(x); // pass a copy of x
printf("In main, x = %d\n", x);
bar(&x); // pass the address of x so we can change it's contents
printf("In main, x = %d\n", x);
return 0;
}
Compiling and running it:
Code:
$ gcc -Wall -g -std=c99 params.c -o params
$ ./params
In main, x = 42
In foo(), x = 17
In main, x = 42
In bar(), x = 17
In main, x = 17