Thread: Pointers to Functions

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    3

    Pointers to Functions

    Can anyone look at this and tell me what is going on?

    I don't understand these statements.

    What I know is, a function address is being stored and then executed via pointer.

    Code:
    typedef    void                            (*pTIC_PROCESS)(void);
    
    void timesliceRegisterTicProcess(INT8U ucProcessNumber, pTIC_PROCESS pProcess)
    {
        sTimeSlice.pTicProcessArray[ucProcessNumber] = pProcess;
    }
    
    (sTimeSlice.pTicProcessArray[0])(); //How this is working is magic to me right now.
    Sorry for the lack of code and description...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > typedef void (*pTIC_PROCESS)(void);
    pTIC_PROCESS is a pointer to a function taking void parameters, returning void.

    > (sTimeSlice.pTicProcessArray[0])();
    Well the first set of parentheses are adding nothing, I would expect this to work also.
    sTimeSlice.pTicProcessArray[0]();

    If you remove the structure and array part of the problem, and just have
    pTIC_PROCESS myfn = sTimeSlice.pTicProcessArray[0];
    myfn();

    is it any clearer?

    This is a good in-depth discussion.
    The Function Pointer Tutorials - Syntax
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    3
    That helps a lot! It is still not 100% clear but at least I know what I am looking for. There is a ton of code like that in there...I will have to get used to it.

    For example,

    Code:
    typedef struct tagMAIL	{
    	INT8U	ucTo;			
    	INT8U	ucMsg;				
    	INT32S	iData;			
     	INT32U	uiData;			
    }	sMAIL,	*psMAIL;
    
    
    typedef void (*pPROCESS)(psMAIL);	// TypeDef of Pointer to function
    
    //Then there is a function like this
    INT8U CreateProcess(pPROCESS pProcess)
    {
    //'''''
    }
    I just can't wrap my head around how that is working.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I think it helps to split the problem into two parts.

    The first is the general weirdness of function pointer declarations due to their length and use of parentheses.
    As a general rule, I always typedef function pointers. It helps to prevent the code getting real messy in a hurry.

    Nobody want's to see this, especially when the function takes more arguments.
    Code:
    INT8U CreateProcess(void (*pProcess)(psMAIL))
    Once you have the typedef, then all declarations become single words as per your example.

    The second part is just using function pointers, which is the easier part.
    Code:
    typedef void (*fn)(char data);
    
    void worker( fn myfunc, char mydata[] ) {
      myfunc(mydata[0]);
    }
    void doit( char data) {
    }
    int main ( ) {
      char line[]="hello";
      worker(doit,line);
    }
    It might help you if you think of functions as 'arrays of instructions' which you 'index' with the () operator.
    Like a regular data array, passing it to a function is just the name of the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    3
    That makes a lot more sense! Thanks much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Muting from functions to pointers to functions
    By Fiskker in forum C Programming
    Replies: 2
    Last Post: 06-18-2015, 12:57 PM
  2. Functions and pointers
    By vidioholic in forum C Programming
    Replies: 8
    Last Post: 10-28-2007, 03:02 PM
  3. Functions and pointers
    By vidioholic in forum C Programming
    Replies: 1
    Last Post: 10-25-2007, 07:46 PM
  4. Functions and pointers
    By vidioholic in forum C Programming
    Replies: 1
    Last Post: 10-25-2007, 07:30 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread