Function Pointers HELP

This is a discussion on Function Pointers HELP within the C++ Programming forums, part of the General Programming Boards category; I want to point a function directly to an address in my program But whenever I write this, void (*foo)(int)= ...

  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
    21,310
    Quote Originally Posted by Swoorup
    I want to point a function directly to an address in my program
    Why?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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 10:08 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21