Function Pointers

This is a discussion on Function Pointers within the C Programming forums, part of the General Programming Boards category; Hello. I haven't had much trouble with function pointers in the past, but now they are a major pain. Could ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    Function Pointers

    Hello. I haven't had much trouble with function pointers in the past, but now they are a major pain. Could anyone tell me what I am doing wrong in the below code and give a fixed version please. I get a compiler error that I will also post below. Thanks in advance.

    Code:
    int test(int x)
    {
        return 0;
    }
    
    int function(char* string)
    {
        //...
        return test; // Returns the address of test.  
    }
    
    int main(void)
    {
        int (*pointer) (int);
        pointer = (void *)function("Hello World"); 
        //...
    }
    Error
    Code:
     error: invalid conversion from `int' to `int (*)(int)'
    How would I fix this. Its really giving me a headache. I've been trying to figure this out for hours now.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    http://www.newty.de/fpt/index.html

    The first thing I would suggest is always create typedefs for your function pointer types. They get pretty hairy at times, and there's always an excess of ( ) to deal with.

    Code:
    typedef int (*fnType)(int);
    
    int test(int x)
    {
        return 0;
    }
    
    fnType function(char* string)
    {
        //...
        return test; // Returns the address of test.  
    }
    
    int main(void)
    {
        fnType pointer;
        pointer = function("Hello World");
        return 0;
    }
    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.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by johnnymckinney2 View Post
    Code:
    int main(void)
    {
        int (*pointer) (int);
        pointer = (void *)function("Hello World"); 
        //...
    }
    You call function(). This returns an int. You cast this to a void * for some reason, then try to stick it in a function pointer. No wonder it doesn't work.

    Code:
    int main(void)
    {
        int (*pointer)(int);
        pointer = (int (*)(int))function;
    }
    Try that.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And of course, assuming that an int is the same size as a pointer may not work right on all systems. It would be better to return a pointer of some type - perhaps a function pointer type - using typedef like Salem suggested, or like this:
    Code:
    int test(int x)
    {
        return 0;
    }
    
    int (*)(int) function(char* string)
    {
        //...
        return test; // Returns the address of test.  
    }
    
    int main(void)
    {
        int (*pointer) (int);
        pointer = function("Hello World"); 
        //...
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 03:22 PM

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