Thread: Casting Problem

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Casting Problem

    I am having a problem casting a void* into a function address:


    void Attempt(void* fun){
    void (*function)(void) = ????;
    function();
    }


    I've tried:

    void (*function)(void) = ((void)(*)(void))fun;

    and even:

    void (*function)(void) = ((void)(*fun)(void));

    ...among others....
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Stupid me, it was:

    void(*function)(void) = (void(*)(void))fun;

    ...it was the extra parenthesis around the first "void"...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Like this maybe?

    Code:
    #include <stdio.h>
    
    void printInt(int value)
    {
       printf("%d\n", value);
    }
    
    void Attempt(void* fun)
    {
       void (*function) (int) = (void(*)(int))fun;
       function(125);
    }
    
    int main()
    {
       Attempt((void*)&printInt);
       return 0;
    }
    EDIT - Damn got beat. I've always hated function pointer syntax.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. type casting problem?
    By lackofcolour in forum C Programming
    Replies: 6
    Last Post: 01-30-2006, 04:29 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM