0% found this document useful (0 votes)
109 views4 pages

C Function Pointer - C Programming

The document discusses function pointers in C programming. A function pointer is a pointer that stores the address of a function. It explains the three steps to use a function pointer: 1) Declare a pointer that can store a function address, 2) Initialize the pointer variable to the function address, 3) Call the function using the pointer variable. Examples are provided to demonstrate function pointers that return values, take parameters, and return pointer types.

Uploaded by

Akanksha ojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views4 pages

C Function Pointer - C Programming

The document discusses function pointers in C programming. A function pointer is a pointer that stores the address of a function. It explains the three steps to use a function pointer: 1) Declare a pointer that can store a function address, 2) Initialize the pointer variable to the function address, 3) Call the function using the pointer variable. Examples are provided to demonstrate function pointers that return values, take parameters, and return pointer types.

Uploaded by

Akanksha ojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

6/1/2019 C function pointer - C Programming - c4learn.

com

 Accessing Integer using char pointer (http://www.c4learn.com/c-programming/pointer-arithmatics-pre-

requisites/)

 C Programming NULL Pointer (http://www.c4learn.com/c-programming/c-null-pointer/)

 C di erence char *a Vs char a[] (http://www.c4learn.com/c-programming/di erence-between-char-

pointer-char-array/)

 C reading complex pointer expression (http://www.c4learn.com/c-programming/c-reading-complex-

pointer-expression/)

 C Pointer Mistake (http://www.c4learn.com/c-programming/c-mistakes/pointer/)

 C Programming dangling pointer (http://www.c4learn.com/c-programming/c-dangling-pointer-causes/)

 C pointer application (http://www.c4learn.com/c-programming/c-pointer-applications/)

 Q1 - which pointer require more space ? (http://www.c4learn.com/c-programming/which-pointer-

require-more-memory-space/)

 Q2 - di erence constant to pointer and pointer to constant ? (http://www.c4learn.com/c-

programming/di erence-between-constant-to-pointer-pointer-to-constant/)

 Add numbers using function pointer (http://www.c4learn.com/c-programming/c-add-two-numbers-

using-function-pointer/)

 Pointer Home (http://www.c4learn.com/index/pointer-c-programming/)

C function pointer

 Previous Page (http://www.c4learn.com/c-programming/c-pointer-to-array-of-string/)


Next Page  (http://www.c4learn.com/c-programming/c-pointer-to-array-of-function/)

What is function Pointer ?


Function pointer : A pointer which keeps address of a function is known as function pointer.
[ Visit : Complete Reference Document for Function Pointer (http://www.c4learn.com/c-
programming/c-function-pointer-reference/) ]

Live Example :

www.c4learn.com/c-programming/c-function-pointer/ 3/8
6/1/2019 C function pointer - C Programming - c4learn.com

#include<stdio.h>

void display();

int main()
{
void *(*ptr)();
ptr = &display;
(*ptr)();

return(0);
}

void display()
{
printf("Hello World");
}

Output :

Hello World

Explanation of C Snippet :
Consider normal program –

Now in the above program we have just called function display(), and we get output as “Hello
World”.

Consider Scenario using pointer , We should follow following 3 steps to use pointer to call function

1. Declare Pointer which is capable of storing address of function.

2. Initialize Pointer Variable


3. Call function using Pointer Variable.

Step 1 : Declaring Pointer

void *(*ptr)();

 We are simply declaring a double pointer.


 Write () symbol after “Double Pointer“.
 void represents that , function is not returning any value.
 () represents that , function is not taking any parameter.

Above declaration tells us ….

www.c4learn.com/c-programming/c-function-pointer/ 4/8
6/1/2019 C function pointer - C Programming - c4learn.com

Declare Pointer variable that can store address of function which does not return anything
and doesn’t take any parameter

Step 2 : Initializing Pointer

ptr = &display;

This statement will store address of function in pointer variable.

Step 3 : Calling a function

(*ptr)();

using (*ptr)() we can call function display();

(*ptr)() = (*ptr)();
= (*&display)();
= (display)();
= display();

thus (*ptr)() is as good as calling function.

Declaration of Initialization of Calling Function


Requirement
Function Pointer Function Pointer using Pointer

Return Type : None


void *(*ptr)(); ptr = &display; (*ptr)();
Parameter : None

Return Type : Integer int result;


int *(*ptr)(); ptr = &display;
Parameter : None result = (*ptr)();

Return Type : Float float result;


float *(*ptr)(); ptr = &display;
Parameter : None result = (*ptr)();

Return Type : Char char result;


char *(*ptr)(); ptr = &display;
Parameter : None result = (*ptr)();

Example 1 : Function having two Pointer Parameters and return type as Pointer

www.c4learn.com/c-programming/c-function-pointer/ 5/8
6/1/2019 C function pointer - C Programming - c4learn.com

#include<stdio.h>

char * getName(int *,float *);

int main()
{
char *name;
int num = 100;
float marks = 99.12;

char *(*ptr)(int*,float *);


ptr=&getName;
name = (*ptr)(&num,&marks);

printf("Name : %s",name);

return 0;
}
//-------------------------------------
char *getName(int *ivar,float *fvar)
{
char *str="www.c4learn.com";
str = str + (*ivar) + (int)(*fvar);
return(str);
}

Output :

.c4learn.com

 Previous Page (http://www.c4learn.com/c-programming/c-pointer-to-array-of-string/)


Next Page  (http://www.c4learn.com/c-programming/c-pointer-to-array-of-function/)

Custom Search

www.c4learn.com/c-programming/c-function-pointer/ 6/8

You might also like