1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

function pointer

Discussion in 'C' started by priyapai, Mar 24, 2006.

  1. priyapai

    priyapai New Member

    Joined:
    Mar 24, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    could any1 plz tell me what does the following statement mean:

    typedef void (*PFNSMSSTATUS)(void * pUser, int status);

    i understand that PFNSMSSTATUS is pointing to a function taking 2 arguments n returning a void.

    What is the significance of having the "typedef"


    -Priya :confused:
     
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    typedef allows you to define the function pointer as PFNSMSSTATUS varName.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    With typedef you can write as

    Code:
    typedef void (*PFNSMSSTATUS)(void * pUser, int status) ;
    PFNSMSSTATUS fptr = func;
    fptr(NULL,1);
    return 0;
    But without typedef it should be
    Code:
    void (*PFNSMSSTATUS)(void * pUser, int status) ;
    PFNSMSSTATUS = func;
    PFNSMSSTATUS(NULL,1);
    return 0;
     
  4. sowmi

    sowmi New Member

    Joined:
    Jun 20, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Even I have a doubt wrt to function pointers,
    whether is it possible to have the function pointers for the functions where the return type and arguements are different???

    Eg :
    int aaa(int a,int b);
    void bbb(void);
    int ccc(char *aa,char *bb,char *cc)

    Thanks,
    Sowmi
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    You must define a function pointer to match each signature.
     
  6. sowmi

    sowmi New Member

    Joined:
    Jun 20, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for instant reply.
    So, are you trying to say that it depends on prototype.??..
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    I'm saying that when you define a function pointer, that pointer is the address of the function. When the call is made, arguments must be passed and a return value (if any) returned.

    Typically, this is done on a stack frame. The address of the function is merely a destination. Key information must be available to the function when that address is reached. The format of that information must obviously be specified, as well as the quantity of information.

    Write a function call in assembly language and you will have a clearer idea of the requirements.
     
  8. sowmi

    sowmi New Member

    Joined:
    Jun 20, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Hi All,

    I am trying to implement the concept of function pointer in C++.
    While compiling i got some errors which I couldnt figure out the reason .
    So,Please let me know what changes should be done for resolving that error.

    I am trying to execute one particular command function when the commandis pressed.Likewise, I have 100 commands and whenever the commands are pressed the corresponding function + arguements should be passed and the function pointer should execute the command.


    Below are my code,

    command.h
    **********
    Code:
    #define FUNCPTR  void *
    #define MAX_NUM_COMMANDS 3
    
    typedef struct
    {
       char command[LINELEN];                           /* name of the command */
       char parm1[LINELEN];                             /*parameter 1 */
       char parm2[LINELEN];                             /*parameter 2 */
                                          
    } ExeCmd_s;
    class FunctionEntry
    {
    
    public:
        char* commandString;
        FUNCPTR pFun;   
        int SearchCommand(char* command);
    };
    FunctionEntry funArr[MAX_NUM_COMMANDS]=
    {
    	    {"aaa",(FUNCPTR)aaa},
                       {"bbb", (FUNCPTR)bbb},
       	    {"ccc",(FUNCPTR)ccc}
    
    };
    enum commandName
    {
    	aaa = 0,
    	bbb
    };
    
    command.c
    **********
    Code:
    void void Execute ()
    {
      .........
      .........
     Execmd execmd;
     FunctionEntry command;
     int rc;
     if(0 != SearchCommand(exeCmd.command))
     {
        command = FunctionEntry.SearchCommand(exeCmd.command);
        switch((int)command.numOfArguements)
        {
             case Zero :
               rc = ((command.pFun)();
               break;
    	 case one:
    	   rc = ((command.pFun),exeCmd.parm1);
    	   break;
        }
     }
     else
     {
       printf("No match");
     }
    
    }
    
    
    
    //Function to search and returns the index of the command
    
    int FunctionEntry :: SearchCommand(char* command)
    {
         
         ExeCmd_s exeCmd;  //already had a Structure where I am extracting the commandname..
         int index = 0;     
         while(index < MAX_NUM_COMMANDS)
         {
    	 if(strcmp(exeCmd.command,funArr[index]) == 0)
    	 {
    	    return &funArr[index];
    	 }
             index++;	
        }
         return NULL;
    }


    Please let me know where i should change the code.
    Expecting some solutions for this code ...

    Thanks,
    Sowmi
     
    Last edited by a moderator: Jun 27, 2007
  9. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    This is bad code:
    Code:
    void void Execute ()
    
    This is bad code:
    Code:
    {
      .........
      .........
    
    Get a different book.
     
  10. sanjaik

    sanjaik New Member

    Joined:
    Sep 23, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Mumbai
    Hi Priya,
    How r u ?
    see this example of function pointer.
    to get easy understand alos clear about
    all fundamention of function pointer.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct strfunptr 
    {
    	float	a;
    	float   b;
    	float (*Func)(float,float);
    
    }sfptr;
    
    float Plus(float a, float b) 
    {
    	return a + b; 
    }
    float Minus(float a, float b) 
    {
    	return a - b; 
    }
    float Mult(float a, float b)
    {
    	return a * b; 
    }
    
    float Div(float a, float b) 
    {
    	return a / b; 
    }
    
    void Switch_With_Function_Pointer(struct strfunptr *);
    
    int main()
    {
    	sfptr *ptr;
    
    	ptr = (struct strfunptr*)malloc(sizeof(ptr));
    	char ch;
    
    	do
    	{
    		printf("1.Add \n");
    		printf("2.Sub \n");
    		printf("3.Mul \n");
    		printf("4.Div \n");
    	
    		printf("Enter the operation Choice\n");
    		scanf("%d",&ch);
    
    		switch(ch)
    		{
    			case 1 : printf(" \n Enter the numbers :");
    					 scanf("%f %f",&ptr->a,&ptr->b);
    					 ptr->Func = Plus;
    					 break;
    
    			case 2 : printf(" \n Enter the numbers :");
    					 scanf("%f %f",&ptr->a,&ptr->b);
    			    	 ptr->Func = Minus;
    					 break;
    
    			case 3 : printf("\n Enter the numbers :");
    					 scanf("%f %f",&ptr->a,&ptr->b);
    					 ptr->Func = Mult;
    					 break;
    			case 4 : printf(" \n Enter the numbers :");
    					 scanf("%f %f",&ptr->a,&ptr->b);
       					 ptr->Func = Div;
    				     break;
    
    			default :
    				printf(" \n Invalid Choice :");
    				exit(0);
    		}
    		Switch_With_Function_Pointer(ptr);
    
    	}while(1);
    
    	return 0;
    }
    void Switch_With_Function_Pointer(struct strfunptr *funptr )
    {
    	float result = funptr->Func(funptr->a,funptr->b);
    	printf("Switch with function result is :%f\n",result);
    } 
    
    Regards
    Sanjai Kumar
    sanjai.kumar@kotak.com
    sanjaysonwani@rediffmail.com


     
    Last edited by a moderator: Aug 14, 2007
  11. sanjaik

    sanjaik New Member

    Joined:
    Sep 23, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Mumbai
    Hi,
    How r u ?
    See this my code, here u need change calling based on swich case
    to make an array that all. enjoy.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct strfunptr 
    {
    	float	a;
    	float   b;
    	float (*Func)(float,float);
    
    }sfptr;
    
    float Plus(float a, float b) 
    {
    	return a + b; 
    }
    float Minus(float a, float b) 
    {
    	return a - b; 
    }
    float Mult(float a, float b)
    {
    	return a * b; 
    }
    
    float Div(float a, float b) 
    {
    	return a / b; 
    }
    
    void Switch_With_Function_Pointer(struct strfunptr *);
    
    int main()
    {
    	sfptr *ptr;
    
    	ptr = (struct strfunptr*)malloc(sizeof(ptr));
    	char ch;
    
    	do
    	{
    		printf("1.Add \n");
    		printf("2.Sub \n");
    		printf("3.Mul \n");
    		printf("4.Div \n");
    	
    		printf("Enter the operation Choice\n");
    		scanf("%d",&ch);
    
    		switch(ch)
    		{
    			case 1 : printf(" \n Enter the numbers :");
    					 scanf("%f %f",&ptr->a,&ptr->b);
    					 ptr->Func = Plus;
    					 break;
    
    			case 2 : printf(" \n Enter the numbers :");
    					 scanf("%f %f",&ptr->a,&ptr->b);
    			    	 ptr->Func = Minus;
    					 break;
    
    			case 3 : printf("\n Enter the numbers :");
    					 scanf("%f %f",&ptr->a,&ptr->b);
    					 ptr->Func = Mult;
    					 break;
    			case 4 : printf(" \n Enter the numbers :");
    					 scanf("%f %f",&ptr->a,&ptr->b);
       					 ptr->Func = Div;
    				     break;
    
    			default :
    				printf(" \n Invalid Choice :");
    				exit(0);
    		}
    		Switch_With_Function_Pointer(ptr);
    
    	}while(1);
    
    	return 0;
    }
    
    void Switch_With_Function_Pointer(struct strfunptr *funptr )
    {
    	float result = funptr->Func(funptr->a,funptr->b);
    	printf("Switch with function result is :%f\n",result);
    } 
    
     
    Last edited by a moderator: Aug 14, 2007
  12. priyapai

    priyapai New Member

    Joined:
    Mar 24, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Hi sanjai,

    Thats a vry good example :) cld u refer me some links to understand mor about callback functions... That wld b vry helpful..

    Thanks in Advance.
     

Share This Page