?
Solved

pointers to functons

Posted on 1998-05-15
5
Medium Priority
?
943 Views
Last Modified: 2012-06-27
I want to maintain an array of pointers to functions(functions of different types). Is it possible to
cast a pointer to void to a pointer to a function.
If yes than how?
0
Comment
Question by:pelegs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
5 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 600 total points
ID: 1250769
It is not allowad by ANSI C but many compilers implement it as an extention.

Example comming...
0
 
LVL 11

Expert Comment

by:alexo
ID: 1250770
   int f1(int x, int y) { return x + y; }
    void f2(float* p) { *p *= 12; }

    typedef int (*PF1)(int, int);
    typedef void (*PF2)(float*);

    void main()
    {
        void* p;
        int i;
        float f = 1.0;

        p = f1;
        i = ((PF1)p)(2, 3);

        p = f2;
        ((PF2)p)(&f);
    }

Under MSVC there are two warnings:
  warning C4152: nonstandard extension, function/data pointer conversion in expression
  warning C4055: 'abstract declarator' : from data pointer 'void *' to function pointer 'void (__cdecl *)(float )'

However, there's a way to avoid them...
0
 
LVL 11

Expert Comment

by:alexo
ID: 1250771
The problem is that ANSI C does not like convertions from data to function pointers and vice versa.  However, if we declare p to be a function pointer (any type) then it is OK.

Example:

    int f1(int x, int y) { return x + y; }
    void f2(float* p) { *p *= 12; }

    typedef int (*PF1)(int, int);
    typedef void (*PF2)(float*);

    void main()
    {
        void (*p)(); /* this line changed */
        int i;
        float f = 1.0;

        p = f1;
        i = ((PF1)p)(2, 3);

        p = f2;
        ((PF2)p)(&f);
    }

0
 
LVL 11

Expert Comment

by:alexo
ID: 1250772
So the general answer is: Yes, if the pointer is of the right type to begin with (not a void pointer but a pointer to some function type).
0
 
LVL 11

Expert Comment

by:alexo
ID: 1250773
1 more to 50K.  Thank you for participating :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question