Solved

pointers to functons

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

691 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