Casting Problem

This is a discussion on Casting Problem within the C++ Programming forums, part of the General Programming Boards category; I am having a problem casting a void* into a function address: void Attempt(void* fun){ void (*function)(void) = ????; function(); ...

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

    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....



    ITSA
    Socket Library!

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

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

    ...it was the extra parenthesis around the first "void"...



    ITSA
    Socket Library!

  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, 05:24 PM
  2. type casting problem?
    By lackofcolour in forum C Programming
    Replies: 6
    Last Post: 01-30-2006, 03:29 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10: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

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