Pointer To Function In C Programming

As we have discussed earlier, a pointer is a variable which holds the address of another variable.

Here in a pointer to function, the address of the variable is passed to a function where the address is caught and used for its operation. The operation executed in that function is done only with the help of the pointer holding the address.

Let us see an example to get the things cleared:

A program to swap the values of two variables using a pointer.

#include<stdio.h>
int main()
{
	int a,b;
	printf("\nEnter the value of the two numbers");
	scanf("%d%d",&a,&b);
	printf("\nThe values before swap: %d %d",a,b);
	swap(&a,&b);
	printf("\nThe values after swap: %d %d",a,b);
	return 0;
}
void swap(int *a, int *b)
{
	int t;
	t=*a;
	*a=*b;
	*b=t;
}

In the above program the address values of variable a and b which holds the actual values are passed in the swap function, where the address values are swapped which ultimately results in the interchange of the actual values and then the variables are printed to get the result of the final operation.

Output:

Wkp2XLS266nK 2Kb8pRLDVDKD8Hoo2t946VhiBmdk5vn68fbPB8FLBzdNsBfmd91Pd8pQ QDJZZSad97TiR38epBi1HyVUXdY9JGgQ9lgu92oMtMWtAhJKE1g2KwlGguB23JHQ

Hence, we come to the short and simple topic of pointer to function. Hope you like it and have cleared all your doubts.

Please comment and share this article if you find it helpful and if you want to improve this article WhatsApp me.

Share Your Love
Default image
Soham Malakar
Hello my name is Soham Malakar am currently pursuing BCA Coding is one of my favorite hobbies. I love to code and an enthusiast to learn different types of programming languages. I love to get intoxicated in the world of technology and video games. A quick learner possessing better results.
Articles: 24

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply