Thread: Function Pointers HELP

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    Question Function Pointers HELP

    I want to point a function directly to an address in my program But whenever I write this,

    void (*foo)(int)= 0x00510102;

    I always get an error in VC++ 2010 saying that a value of type int cannot be used to initialize an entity of type "void(*)(int)"

    What am I doing wrong. I have tried many things but I just cant simply get the function pointer to point to 0x00510102 address.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    I want to point a function directly to an address in my program
    Why?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I won't even ask why, but that solution is this:

    Code:
    void (*foo)(int) = (void (*)(int))0x00510102;
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Oh thank you sir

    EDIT:Actually I am trying to manipulate some features of a game by its own functions. It is a project actually.
    Last edited by Swoorup; 11-16-2011 at 11:08 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, another wannabe "hacker".
    Closed - and read the rules!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. Help with function pointers
    By slowpro in forum C Programming
    Replies: 3
    Last Post: 10-10-2006, 02:50 PM
  5. Pointers to function(function pointers)
    By abhishek_charli in forum C Programming
    Replies: 4
    Last Post: 06-23-2002, 01:24 AM