How2Lab Logo
tech guide & how tos..


Pointers in C - Part 6 of 9


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:

<datatype> (*fp)();

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;
}

Exercises

Give a suitable declaration for each of the following cases:

  1. Declare two pointer variables which point to integer variables i and j.
  2. Declare a function that accepts two integer arguments and returns a long integer. Each argument will be a pointer to an integer quantity.
  3. Declare a one dimensional, floating point array of long integer using a pointer.
  4. Declare a two-dimensional floating point array having 10 rows and 20 columns, using pointers.
  5. Using pointers, declare an array of strings whose initial values are black, yellow, and red.

Lab Work

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:

Computer is a machine. Human is not. Human makes mistakes. Computer cannot.

Share:
Buy Domain & Hosting from a trusted company
Web Services Worldwide
About the Author
Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

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.


Refer a friendSitemapDisclaimerPrivacy
Copyright © How2Lab.com. All rights reserved.