Solved

pointers to functons

Posted on 1998-05-15
5
887 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
  • 5
5 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 200 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

815 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now