29)

Parameter passing and pointers again

Program 26 Parameter passing and pointers again Source code - prog26.c
The program shown on this page is pretty useless as a program, but it does show clearly the difference between passing parameters either by value or by reference. As this program involves just a single source code file you can use Miracle C or any compiler.

 

Program code

#include <stdio.h>
#include <conio.h>

void passbyval(int num)
{
num+=10;
}

void passbyref(int *num)
{
*num+=10;
}

void main(void)
{
int mynum=20;

passbyval(mynum);
printf("The value of mynum is still %d after the passbyval call\n",mynum);
passbyref(&mynum);
printf("The value of mynum is %d after the passbyref call\n",mynum);
getch();
}

 

Description of the program code
First a variable is declared which is local to the main function, called mynum. The value stored in the variable is initialised to 20 when it is declared. Because mynum is declared as a local variable of the main function, no other functions of the program have access specifically to it. In other words, they don't know nothing of it, and hence can't use the variable. There is a way around this though, in fact there are a few ways around. The easiest way of all is to declare the variable as a global variable, but this isn't considered good programming practice again :-(.Another way is to pass a pointer to the variable to a function, which will tell the function of exactly where it is stored in memory, enabling it to access it.

The normal way that parameters are passed is by value, which means that the value of a variable is passed to a function, not the memory address of where the variable is stored. The function then receives this value, and in effect, creates it's own local variable to the function. Any changes made to this local variable will only apply to the local variable in the function, not to the variable that has come from the sending function.

This is shown in the first few lines of the program above. The passbyval function is called, passing the value (20) of the mynum variable ( local to main ) to it.The passbyval function receives the value 20 and creates its own local variable called num. 10 is then added to the num variable which changes it to 30 and the function ends. When the program returns back to main, the value of the mynum variable is displayed, and shows that it is still 20 not 30 ! This is because only the local variable num in the passybyval function has been changed, not the mynum variable in the main function.

The passbyref function though, does change the value of the mynum variable. It does this because the function is passed a pointer to the mynum variable, not the value of the variable. The & in front of mynum, translates to Address of. The num variable is still local to the passbyref function, but this time it is a pointer to a memory address, and *num+=10; adds 10 to the contents of that memory address. The memory address is the position where the variable mynum ( local to main ) is stored in memory, so this will change it.

 

Summary
This page, showed the difference between passing the value of a variable to a function, and passing a pointer to a variable to a function. The example program shown isn't much use though, but it did show how the contents of a local variable can be altered from another function, by passing a pointer to the variable.

The output of the above program looks like below :

The value of mynum is still 20 after the passbyval call
The value of mynum is 30 after the passbyref call

After that brief rest, it's that time again to move onto something a bit more complex and very useful, Linked lists.

 

Tasks

No tasks for this page.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001