Solved

pointers to functons

Posted on 1998-05-15
5
873 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements 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.

706 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

17 Experts available now in Live!

Get 1:1 Help Now