C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Pointer and Functions

A function pointer is nothing more than a variable that stores the address of a function. Once the address of a function is assigned to a pointer variable (function pointer), Then the respective function pointer can be used to access the function. Following simple program can clearly demonstrate the function pointer.

function pointer in c

C Program - Function Pointer Without Return Type

pointer-functions-1.c
#include <stdio.h>
void function(); // function declaration
int main()
{
void (*ptr)(); // function pointer declaration
ptr = &function; // function pointer initialization
(*ptr)(); // Using function pointer
return 0;
}
void function() // function definition
{
printf("Access me with my address");
}
  • Access me with my address

Note:

In the above pointer to function program without any return type, we used void pointer (*ptr)() to hold the address of the starting point of the function, then this void pointer is used in line 7 to access the function.

C Program - Function Pointer With Return Type

pointer-functions-2.c
#include <stdio.h>
int function(); // function declaration
int main()
{
int (*ptr)(); // function pointer declaration
ptr = &function; // function pointer initialization
printf("function pointer returns %d",(*ptr)()); // Using function pointer
return 0;
}
int function() // function definition
{
int c = 4 + 4;
return c;
}
  • function pointer returns 8

Note:

In the above pointer to function program with return type, we used integer pointer (*ptr)() instead of void pointer to hold the address of the starting point of the function, Then this integer pointer is used to invoke the function to return the summation value of 4 + 4.

C Program - Passing Value Using Function Pointer

pointer-functions-3.c
#include <stdio.h>
int function(); // function declaration
int main()
{
int (*ptr)(); // function pointer declaration
ptr = &function; // function pointer initialization
printf("function pointer returns %d",(*ptr)(10)); // Using function pointer
return 0;
}
int function(int i) // function definition
{
int c = i + 4;
return c;
}
  • function pointer returns 14

Note:

The above program demonstrate that a function which is invoked by a pointer variable can be used to pass value to the function to perform some operation.

C Program - Passing Address Using Function Pointer

pointer-functions-4.c
#include <stdio.h>
int function(); // function declaration
int main()
{
int (*ptr)(), i = 6, *iptr; // function pointer declaration
iptr = &i;
ptr = &function; // function pointer initialization
printf("function pointer returns %d",(*ptr)(iptr)); // Using function pointer
return 0;
}
int function(int *i) // function definition
{
int c = *i + 4;
return c;
}
  • function pointer returns 10

Note:

The above program demonstrate that a function which is invoked by a pointer variable can be used to pass address of another variable to the function to perform some operation.

Did you know?

Function pointer just holds the address of staring point of a function but not the entire address in an array format.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report