Well, you don't have to return anything to manipulate exterior data. This is the fun of pointers.
When you pass an argument to a function, you pass one of two things:
a) A copy of the data.
b) The location of the actual data.
The first is called passing by value.
The second is called passing by reference.
The first, you pass a copy of the data, like so:
Code:
#include <stdio.h>
void fun1( int x )
{
printf("fun1 - x is %d", x );
x++;
printf("fun1 - x is %d", x );
}
int main( void )
{
int var;
var = 10;
printf("main - var is %d", var );
fun1( var );
printf("fun1 - var is %d", var );
return 0;
}
Here, what happens is that a copy of the value of 'var' is put into the variable x in 'fun1'. As such, when the function is done, the original value in 'var' isn't ever changed.
Now look what happens when we use a pointer to point at the real data rather than a copy of it:
Code:
#include <stdio.h>
void fun1( int *x )
{
printf("fun1 - x is %d\n", *x );
(*x)++;
printf("fun1 - x is %d\n", *x );
}
int main( void )
{
int var;
var = 10;
printf("main - var is %d\n", var );
fun1( &var );
printf("fun1 - var is %d\n", var );
return 0;
}
Here we pass the address of 'var' to the function so that whatever happens in 'fun1' actually effects the real variable 'var'.
Quzah.