Wednesday, 4 April 2012

Arguments passing by Reference, Arrays of Pointers


Arguments passing by Reference:-
          In passing by reference method, the addresses of actual arguments in the calling function are copied into formal arguments in the called function. Passing an argument by reference instead of by value offer an advantage as well as a disadvantage. These are:-
The advantage is that the function can be used to modify the value of the argument variable.
The disadvantage is that the function can modify the value of the argument variable, which may not be desired in some cases.
          When we pass arguments by reference, we must ensure that the function definition and proto type reflect the fact that the argument passed to the function is a pointer. Within the body of the function, we must use the indirection operator to access the value of the variable passed by reference.
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
a=2;
b=3;
inter (&a,&b);
{
printf(“\n a=%d”,a);
printf(“\n b=%d”,b);
}       
inter( int *x, int *y);
{
int  temp;
temp=*x;
*x=*y;
*y=temp;
printf(“\n x=%d”,*x);
printf(“\n y=%d”,*y);
}

Arrays of Pointers:-
          As these are arrays of ints, or of float, similarly also. As a pointer variable always of pointers is collection of address. The most common use of an array of pointers in with strings.
          Syntax of declaration:-
                                      datatype  *pointer_var[index];
          Ex:-           char  *ar_p[5];

No comments:

Post a Comment