Click here to Skip to main content
Click here to Skip to main content

Tagged as

Function Pointer in C Struct

, 25 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Using the Function Pointer inside C struct


Pointers form very important part of C language, so the solid understanding of the pointers and the effectively in using them will enable the programmer to write more experienced programs.We should always remeber that the pointer is variable hold memory address.  Pointer can refer to  usual data type like int, char, double and etc . For example when you type 

int * intptr;     // declare pointer to integer value 

int intval = 5 ;  // declare integer value 

intptr = & intval ; // intptr now include the memory address of the intval

Not only regulare data type but also pointer can point to functions.

Function Pointers

The function pointer is a pointer hold the address of the function. Since C is not OOP language I Consider the Function Pointer as the Father of virtual functionality in the modern languages. In the oop language each driven class will  implements the virtual method depend on its need. In the C it is something similar, since we give the function pointer the address of the desired function implementation.


To declare function pointer we have to follow the next syntax 

Return Type       * function pointer's variable name ) ( parameters 

The declaration of function pointer called func which accept two integer parameters and return an integer value will be like next:

int (*func)(int a , int b ) ; 

It is convenient to declare a type definition for function pointers  like:

typedef int (*func)(int a , int b ) ; 

Function Pointer in Struct

Stuct in C used to represent data structure elemenst, such as student data structure. Struct can contian varible from simple data type and others  from complex ones. complex data type such as varible of function pointer. The easy way to explain the programming ideas by give some simple and suffecient code, Let is start by defining a function pointer and  simple struct.

We define first an function pointer called Operation which return an int value and accepts two integer parameters

 typedef   int (*Operation)(int a , int b );

Let us also have a simple struct STR which contains pointer to the Operation function pointer and an integer variable to store the returned value from the Operation variable:

typedef  struct _str {

       int  result ; // to sotre the resut
       Operation  opt; // funtion pointer 

 } STR;

Now we will define tow function Add and Multi , each of them retuen integer value and accept two integer parameters.

* Add two numbers a and b 

int Add ( int a , int b ){
      return a+b ;

* Multiple two numbers a and b 

int Multi ( int a , int b ){

       return a*b ;

Now we can write the main method and learning how to assign the function pointer to the proper function

int main (int argc , char **argv){

      STR str_obj;

      str_obj.opt = Add;//the function pointer variable point to Add function 

      str_obj. result = str_obj.opt(5,3);

      printf (" the result is %d\n", str_obj.result );

      str_obj.opt= Multi;//the function pointer variable point to Multi function 

      str_obj. result = str_obj.opt(5,3);

      printf (" the result is %d\n", str_obj.result );

      return 0 ;


The results will be :

   the result is 8

   the result is 15 


Function pointer is c technique which  enable the programmer to controlling the execution sequence within an application by allowing alternate functions to be executed based on the application’s needs.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Germany Germany
Seek knowledge from the cradle to the grave

Comments and Discussions

GeneralMy vote of 5 PinmemberRadu_Socaciu2-Aug-14 1:29 
GeneralRe: My vote of 5 PinmemberMohammad_Hamad2-Aug-14 9:05 
QuestionFunction Pointer in C PinmemberMember 1090401629-Jul-14 18:26 
AnswerRe: Function Pointer in C PinmemberMohammad_Hamad29-Jul-14 22:30 
SuggestionI think .... PinmemberCristian Amarie25-Jul-14 4:38 
... this is better.
Especially the use of vtbl instead of separated function pointers:
struct client_t {
  void (* __ctor)(struct client_t* self, int argc, wchar_t** argv);
  void (* __dtor)(struct client_t* self);
  int  (* run)(struct client_t* self);
but better
/* client.h */
struct client_t {
  /* functions */
  struct client_vtbl_t {
    void (* __ctor)(struct client_t* self);
    void (* __dtor)(struct client_t* self);
    int (* run)(struct client_t* self);
  } *vtbl;
  /* data */
  int n;
  char* s;
/* client.c */
static void client___ctor(struct client_t* self) {
  self->n = 0;
  self->s = NULL;
static void client___dtor(struct client_t* self) {
  if(self->s != NULL) {
static int client_run(struct client_t* self) {
  /* implement run */
  /* destroy */
int client__new(struct client_t** cl) {
    struct client_t* c;
    /* argcheck */
    if(cl == NULL) {
      return EINVAL;
    *cl = NULL;
    /* alloc object */
    c = (struct client_t *)malloc(sizeof(*c));
    if(c == NULL) {
      return ENOMEM;
    /* alloc and setup vtable */
    c->vtbl = (struct client_vtbl_t *)malloc(sizeof(*c->vtbl));
    if(c->vtbl == NULL) {
      return ENOMEM;
    c->vtbl->__ctor = &client___ctor;
    c->vtbl->__dtor = &client___dtor;
    c->vtbl->run    = &client_run;
    /* call constructor */
    (* c->__ctor)(c);
    /* set output and return */
    *cl = c;
    return 0;

GeneralThis is the path to C++ PinmemberJohn Brett25-Jul-14 3:31 
GeneralRe: This is the path to C++ [modified] PinmemberMohammad_Hamad25-Jul-14 5:01 
SuggestionRe: This is the path to C++ PinmemberCaFeSito28-Jul-14 11:55 
GeneralRe: This is the path to C++ PinmemberMohammad_Hamad28-Jul-14 12:33 
GeneralRe: This is the path to C++ PinmemberCaFeSito28-Jul-14 12:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 25 Jul 2014
Article Copyright 2014 by Mohammad_Hamad
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid