Professional Documents
Culture Documents
C Programming Tutorial: Function Pointer in C
C Programming Tutorial: Function Pointer in C
C Programming Tutorial: Function Pointer in C
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
//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
//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
Call by Reference
http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx
7/23/2016
C Function Pointer
Page 6 of 10
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.
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
//Statement 1
//Statement 2
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.
http://www.tutorialdost.com/C-Programming-Tutorial/19-C-Pointer-Function.aspx
7/23/2016