Pages

Pointers and functions

Pointer as an argument

Pointers can also be passed as arguments to a function as normal values are being passed. This method is also known as call by reference. By using these pointers, the calling function’s arguments can be changed from called function. Whenever we want to pass a pointer as an argument, these things should be carried out:

In function prototype, place an * after the type of argument to specify that we are passing a pointer as an argument.

In function call, place the argument- the address of operator followed by name of argument or the pointer that is declared earlier.

Develop the function definition based on the specified prototype.

Write a program to increment three values from called function and print them in calling function.

#include

$ < $stdio.h$ > $

void increment(int *,int *,int *); //function prototype

main()

{

int a=20,b=30,c=40;

printf(“\n Before increment a=%d\tb=%d\tc=%d”,a,b,c);

increment(&a,&b,&c); //function call

printf(“\n After increment a=%d\tb=%d\tc=%d”,a,b,c);

}

void increment(int *x,int *y,int *z) //function definition

{

++*x;

++*y;

++*z;

}

* Indirectly, a called function can return multiple values if pointers to these are passed as arguments.

Pointer as a return value

Pointers can also be returned to the calling function from called function as normal values. When we want to return a pointer to the calling function from called function, we should do these:

In function prototype, place an ‘*’ after the return type to indicate that the function returns a pointer.

In calling function, there should be a pointer variable that is declared to hold the pointer that is returned. However, both types should be the same to avoid ambiguities.

üDevelop the function definition as specified in function prototype. In function definition, the return statement, at end, should consist of an ‘&’ before the return value or a pointer that is declared earlier.

* Returning a pointer to local argument of called function to calling function gives you a warning.

A pointer-to-function can be passed to another function as an argument. This allows one function to be transferred to another function. Let us refer to the first function as the guest function and the second function as host function. Thus the guest function is passed to the host function, where it can be accessed. When we want to pass a function as an argument to another function, do the following:

1)While calling the host function, place the name of the guest function as an argument. The name itself serves as a pointer to guest function.

2)Give the definition of guest function as usual.

3)In the definition of host function, place a pointer to guest function as an argument.

4)In the definition of host function, call guest function for further process.

Program that demonstrates function as an argument to another function

#include$ < $stdio.h

$ > $

int add(int,int); //guest function prototype

void swap(int(*)(int,int));//host function prototype

int a,b;

main()

{

printf("\n Enter any two numbers:");

scanf("%d%d",&a,&b);

swap(add); //step-1

printf("\n After swap a=%d\tb=%d",a,b);

}

int add(int x,int y) //step-2

{

return x+y;

}

void swap(int (*addptr)(int,int)) //step-3

{

a=(*addptr)(a,b); //step-4

b=(*addptr)(a,-b); //step-4

a=(*addptr)(a,-b); //step-4

}

Output:

Enter any two numbers: 32

45

After swap a=45 b=32

Casting pointers

A pointer always has a type associated with it.As we can convert a variable of one type to another, we can also convert one type of pointer to another type of pointer. This process of conversion is called as casting pointers. Unlike variables, we can’t assign one type of pointer variable with another type of pointer variable, although both of them have memory addresses as their values. This is known as incompatibility of pointers.

We can’t use the assignment operator with the pointers of different types. We can, however, make explicit assignment between two incompatible pointer types by using cast operator, as we do with fundamental types. The cast operator can be used as follows:

$ < $ptr1$ > $=($ < $data type$ > $ *)$ < $ptr2$ > $;

In this syntax, ptr2 is a pointer variable of a data type that is going to be converted to the type of ptr1. Usually, the is same as the type of