Write about Functions using Pointers as Arguments?

When an array is passed to a function as an array , only the add of the 1st element of the array is passed, but not the actual values of the array elements.

Ex:–

sort(x);
sort(int x[ ] );

If x is an array when we call sort (x), The address of x[0] is passed to the function sort.

The function uses address for manipulating the array elements. when we pass address to a function the parameters receiving the address should be pointers.

the process of calling a function using pointers to pass the addresses of variables is known as ‘call by reference’.

Ex:–
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
x=20;
change(&x);
printf(“%d\n”, x);
getch();
}
change (int *P)
{
*P=*P+10;
}

When the function changes() is called the add of the variable x, not its value, is passed into the function change().

In side change(), the variable p is declared as a pointer and there four, P is the address of the variable x.

The statement  *p=*p+10;

Means add 10 to the value stored at the address p.since p represents the address of x, the value of x is changed from 20 to 30.

Thus, call by reference providers a mechanism by which the function can change the stored values in the calling function.

Note that this mechanism is also known as ‘call by address’ (or)’ pass by pointers’.

You may note the following points:

1. The function parameters are declared as pointers.

2. The dereferensed pointers are used in the function body.

3. When the function is called, the add.es are passed as function body.

The process of calling a function to pass actual values of variables is known as ‘call by value’.

Functions Returning Pointers:

A function can return a single value by its name (or) return multiple values through pointers parameters.

We can also force a function to return a pointer to the calling function.

Ex:–
#include<stdio.h>
#include<conio.h>
int *larger(int *,int *);
void main()
{
int a=10, b=20*p;
p=larger(&a, &b);
printf(“%d”, *p);
}
int *larger(int *x, int *y)
{
if (*x>*y)
return(x);      \\address of a
else
return(y)         \\address of b
}

Note that the address returned must be the address of a variable in the calling function. It is an error to return a pointer to a variable in the called function.

Pointers to Functions:

A function like a variable has a type & an add. Location in the memory. It is there four, possible to declare a pointer to a function, which can then be used as an argument in the another function.

A pointer to a function is declared as follows:

            type (*fptr)();

® This tells the compiler that fptr is a pointer to a function, which returns type value.

®The parentheses around *fptr are necessary.

®Remember that a statement like
    type *gptr();
    Would declare gptr as a function returning a pointer to type.

®We can make a function pointer to point to a specific function by simply assigning the      name of the function to the pointer.

Ex:– double mul(int, int);
            double (*p1)();
            p1=mul;

declare p1 as a pointer to a function and mul as a function & then make p1 to the function mul.To call the function mul, we may now use the pointer p1 with the list of parameters. i.e.,

(*p1)(x, y)   //function call
is equivalent to  mul (x, y);

note the parenthesis around *P1

Related

C Language 4357675855186959338

Post a Comment

emo-but-icon

item