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 program that demonstrates pointer as return value

#include$<$stdio.h$>$
main()
{
int a,b;
int *c;
printf(“\n Enter the values of a and b: ”);
scanf(“%d%d”,&a,&b);
printf(“\n The result=%d”,*c);
}
int * add(int x,int y) //function definition
{
int z;
z=x+y;
return (&z);
}

Output:
Enter the values of a and b: 10
12
The result=22

Pointer-to-function

We can also call a function using a pointer. To call a function using a pointer, do the following:
1. First, declare a pointer to function that has the following form:
$<$return type$>$ (*$<$name_of_ptr)(arg_type_1,arg_type_2,…);

The above declaration tells that addptr is a pointer to a function that takes two integer arguments and returns an integer value.

1. Assign the name of the function to that pointer. The name of the function itself acts as an address of function.

3. Call the function by using pointer in the same way as we call a function.
The above statement is used to call that function which is already assigned to pointer-to-function.

A program that demonstrates pointer-to-function

#include$<$stdio.h$>$
int add(int,int); //function prototype – No change, give prototype normally.
main()
{
int a,b,c;
printf(“\n Enter the values of a and b: ”);
scanf(“%d%d”,&a,&b);
c=(*addptr)(a,b); //step-3 call the function using pointer
printf(“\n The result=%d”,c);
}
int  add(int x,int y) //function definition- No change, give definition normally
{
int z;
z=x+y;
return z;
}

Output:
Enter the values of a and b: 10
12
The result=22

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$>$
void swap(int(*)(int,int));//host function prototype
int a,b;
main()
{
printf("\n Enter any two numbers:");
scanf("%d%d",&a,&b);
printf("\n After swap a=%d\tb=%d",a,b);
}
{
return x+y;
}
{
}

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 $<$ptr1$> Program that demonstrates casting from one type to another #include$ < $stdio.h$ > \$
main()
{
int a=65;
int *iptr=&a;
char *cptr;
cptr=(char *)iptr;
printf("%c",*cptr);
}
Output:
A

 Follow: Share: