¤ 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


Download Mobile App for Android


Web Service Offers

.CLUB Registration
1,199 199 (83.4% off)
per Year

Ecommerce on the Cloud

An online store hosted on the Cloud. Sell unlimited products. Integrated business functions. Well-suited for high traffic website.

39,999 29,999 (25% off)

Ecommerce Premium

An e-commerce website well-suited for companies having a large portfolio of products. Sell unlimited products. Manage both offline and online sales from one system.

40,000 25,000 (37.5% off)

Ecommerce Business

Launch your online business. Sell up to 5000 products. Collect payment via multiple online and offline payment methods.

35,000 25,000 (28.57% off)

Ecommerce Pro

Get a Mobile friendly online store front. Sell up to 1000 products.

35,000 25,000 (28.57% off)

Ecommerce Starter

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

35,000 25,000 (28.57% off)

Website on the Cloud

Custom built, responsive corporate website hosted on the cloud. Mirrored on 3 servers. Includes visitor signup with CRM functions/

19,999 9,999 (50% off)

Website Corporate

Custom built, responsive corporate website with CRM. Our most popular website plan.

19,000 9,000 (52.63% off)

Website Business - 1000 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

18,000 8,000 (55.56% off)

Website Business - 500 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,500 7,500 (57.14% off)

Website Business - 100 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,000 7,000 (58.82% off)

Website Business - 50 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,500 6,500 (60.61% off)

Website Business - 25 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,000 6,000 (62.5% off)

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% 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