Since C function names essentially store addresses, you can easily use a pointer to point to a function. The function address can be assigned to a pointer to a function type and the function can indirectly be invoked using that pointer.
The syntax for declaring a function pointer is as given below:
where <datatype> is the type of the data returned by the function pointed to by fp.
Here is a small program which you can run to verify that the name of a function indeed stores its address:
/* Program to display the address of a function */ #include <stdio.h> main() { void warning(); printf("\nAddress of function warning() is: %u\n", warning); warning(); } void warning() { printf("Don't go out in the rain!!\n"); return; }
A typical output of the above program will be:
Address of function warning() is: 450234 Don't go out in the rain!!
In the above program, the function declaration void warning(), that appears within main(), is actually a pointer to that function. So assigning the function address to a pointer will help in invoking the function indirectly. The following example illustrates the technique:
/* Program to display the address of a function */ #include <stdio.h> main() { void warning(); void (*fptr)(); //defines a pointer to a function returning void (or no data) fptr = warning; //assigns the address of the function to fptr printf("\nAddress of function warning() is: %u\n", fptr); (*fptr)(); //this statement indirectly invokes the function warning() } void warning() { printf("Don't go out in the rain!!\n"); return; }
Give a suitable declaration for each of the following cases:
Write a C program to accept three strings from the user say string1, string2 and string3. Search for the occurrence of string2 in string1. If it is found, replace the occurrence of string2 by string3.
For example, when the user provides the following strings:
String 1: Computer is a machine. Man is not. Man makes mistakes. Computer cannot.
String 2: Man
String 3: Human
The resulting string will be:
How to move your Email accounts from one hosting provider to another without losing any mails?
How to resolve the issue of receiving same email message multiple times when using Outlook?
Self Referential Data Structure in C - create a singly linked list
Mosquito Demystified - interesting facts about mosquitoes
Elements of the C Language - Identifiers, Keywords, Data types and Data objects
How to pass Structure as a parameter to a function in C?
Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.
Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.