¤ Home » Programming » C Tutorial » Pointers in C - Part 6 of 9

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>
	void warning();
	printf("\nAddress of function warning() is: %u\n", warning);

void warning() 
	printf("Don't go out in the rain!!\n");

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>
	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");


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.


comments powered by Disqus

Download Mobile App for Android

Web Service Offers

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting included.

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 XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.

Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy