¤ 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>
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:




comments powered by Disqus


Buy Domain & Hosting from the most reliable and trusted company - WebServicesWorldWide.com.

Looking to build a website?
Launch a 20 page website in 1 day at only Rs.200/month or US$ 3.19/month. Hosting & Email 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