C Programming Tutorial: Function Pointer in C

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

C Function Pointer

Page 1 of 10

(../index.html)

C Programming Tutorial
PROGRAMS (../SHOWPROGRAM.ASPX)

Advertisements

Function Pointer in C
Like normal variable, pointer variable can be passed as function argument and function can
return pointeras well.
There are two approaches to passing argument to a function:

Call by Value
Call by Reference / Address
Call by Value
In this approach, the values are passed as function argument to the definition of function.

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

C Function Pointer

Page 2 of 10

Example
#include<stdio.h>
void fun(int,int);
void main()
{
int A=10,B=20;
printf("\nValues before calling %d, %d",A,B);
fun(A,B);

//Statement

printf("\nValues after calling %d, %d",A,B);


}
void fun(int X,int Y)
{
X=11;
Y=22;
}

//Statement

Output :
Values before calling 10, 20
Values after calling 10, 20

In the above example, statement 1 is passing the values of A and B to the calling function fun
(). fun() will recieve the value of A and B and put it into X and Y respectively. X and Y are
value type variables and are local to fun(). Any changes made by value type variables X and
Y will not effect the values of A and B.

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

C Function Pointer

Page 3 of 10

Call by Reference
In this approach, the references/addresses are passed as function argument to the definition
of function.

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

C Function Pointer

Page 4 of 10

Example:
#include<stdio.h>
void fun(int*,int*);
void main()
{
int A=10,B=20;
printf("\nValues before calling %d, %d",A,B);
fun(&A,&B);

//Statement

printf("\nValues after calling %d, %d",A,B);


}
void fun(int *X,int *Y)
{
*X=11;
*Y=22;
}

//Statement

Output :
Values before calling 10, 20
Values after calling 11, 22

In the above example, statement 1 is passing the reference of A and B to the calling function
fun(). fun() must have pointer formal arguments to recieve the reference of A and B. In
statement 2 *X and *Y is recieving the reference A and B. *X and *Y are reference type
variables and are local to fun(). Any changes made by reference type variables *X and *Y will
change the values of A and B respectively.

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

C Function Pointer

Page 5 of 10

Difference b/w Call by Value and Call by Reference


Call by Value

The actual arguments can be variable or


constant.

Call by Reference

The actual arguments can only be variable.

The values of actual argument are sent to

The reference of actual argument are sent to

formal argument which are normal variables.

formal argument which are pointer variables.

Any changes made by formal arguments will

Any changes made by formal arguments will

not reflect to actual arguments.

reflect to actual arguments.

Function Returning a Pointer


Like normal variable, a function can also return reference or address. When function returns
reference or address, the return type of function must be pointer type.

Syntax for function returning pointer:


return-type *function-name(argument list)
{
---------body of function
---------}

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

C Function Pointer

Page 6 of 10

Example for function returning pointer:


#include<stdio.h>
int *reference(int);
void main()
{
int A=10;
int *ptr;
printf("\nAddress of %d in main() is %u",A,&A);
ptr = reference(A);
printf("\nAddress of %d in reference() was %u",A,ptr);
}
int *reference(int n)
{
return &n;
}
Output :
Address of 10 in main() is 8736
Address of 10 in reference() was 8730

Pointer to Function
Like normal variable, Every function has reference or address, and if we know the reference
or address of function, we can access the function using its reference or address. This is the
another way of accessing function using pointer.

Syntax for declaring a Pointer to function:


return-type (*ptr-function)(argument list);

return-type : type of value function will return.


argument list : represents the type and number of value function will take, values are sent by
the calling statement.

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

C Function Pointer

Page 7 of 10

(*ptr-function) : The parentheses around *ptr-function tells the compiler that it is pointer to
function.
If we write *ptr-function without parentheses then it tells the compiler that ptr-function is a
function that will return a pointer.
The pointer to function will be accessed as:
(*ptr-function)(val1,val2...n);

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

C Function Pointer

Page 8 of 10

Example : Sum of two numbers using pointer to function.


#include<stdio.h>
int Sum(int,int);
int (*ptr)(int,int);
void main()
{
int a,b,rs;
printf("\nEnter 1st number : ");
scanf("%d",&a);
printf("\nEnter 2nd number : ");
scanf("%d",&b);
ptr = Sum;
rs = (*ptr)(a,b);

//Statement 1
//Statement 2

printf("\nThe sum is : %d",rs);


}
int Sum(int x,int y)
{
return x + y;
}
Output :
Enter 1st number : 78
Enter 2nd number : 45
The sum is : 123

In the above program, Statement 1 is assigning the address of Sum() to pointer ptr. Statement
2 is calling and passing two values to Sum().

Related topics

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

C Function Pointer

Page 9 of 10

- C storage classes, Local variable, Global variable, External variable, Register variable. (14C-Storage-Classes.aspx)
- C String, Input string using getche(), scanf(), gets() (20-C-String.aspx)
- What is Structure in C? (21-C-Structure.aspx)
- Passing & Returning Structure from Function? (22-C-Structure-Function.aspx)
- Explain bit field in c with example? (25-C-Bit-Field.aspx)
Advertisement

Follow us

(C-Programming-Introduction.aspx) (C-Programming-Introduction.aspx)
(C-Programming-Introduction.aspx) (C-Programming-Introduction.aspx)
Advertisements

Site Map
Learn C Programming
Learn C++ Programming
Learn Core Java

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

C Function Pointer

Page 10 of 10

Learn VB.NET
Learn C Sharp.net

Examples
C Examples
C++ Examples
Core Java Examples
Visual Basic.NET Examples
C Sharp.net Examples

About Us
This website is designed for readers who have less or no programming experience. Even
the experienced programmers will find this website equally useful. We have covered all
the basic of C, C++, C#, JAVA, VB.NET, ASP.NET, etc..., programming language with easy
examples and their descriptions.

COPYRIGHT 2084 COMPANY NAME

http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx

7/23/2016

You might also like