CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 1999
    Location
    Mumbai, India
    Posts
    207

    Post On assignment to a function pointer

    Hi,

    I have a the following declarations in my C code:

    PHP Code:
    void func1(void);    // Prototype of function func1()

    long *= ...        // pointer to long which contains a valid address.
                         // The address is that of the function func1()
                         
    void (*fptr)(void);  // This is the function pointer 
    This address in 'p' is, as stated above, the address of the function func1().

    How do I assign from pointer 'p' to pointer 'fptr' so that the
    function executes if called as given below:

    PHP Code:
    fptr(); 
    Thanks in advance.
    Thank you.
    Sayan
    ====================

    Sayan Mukherjee
    [Email: [email protected]]

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Just do fptr = p

    That will of course give a warning or two, unless you cast it: fptr = (void (*)(void))p.

    I don't know why you need to store the func address in 'p' when you can assign fptr directly: fptr = func1. That's better coding style.

    Good luck.

  3. #3
    Join Date
    Oct 1999
    Location
    Mumbai, India
    Posts
    207

    Post Re: Function pointer

    Hi j0nas,

    Thanks for the reply. It really helped.

    The code in the posting is a curtailed version of the original
    code where I had the doubt.

    In the actual code, I am getting a pointer as long * (from
    another part of the application) containing the function's
    address. I need to execute that function from this address.

    Thanks again for your help.
    Thank you.
    Sayan
    ====================

    Sayan Mukherjee
    [Email: [email protected]]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured