Thread: on funtion pointers

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43

    on funtion pointers

    void *ptr;

    what is the correct way to cast "ptr" in the code above to be a pointer to a 3 element array of function pointers with a return value of int and a single parameter of a pointer to char.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I don't believe function pointers exist in C. You may want to check the C++ board.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    No as per my knowledge there are function pointers in C. it is defined like int (*func)(int, int)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I don't believe function pointers exist in C.
    Of course they do - just look at qsort()

    > what is the correct way to cast
    It's not guaranteed that you can cast a data pointer (even void*) to and from a function pointer.
    But the first step would be to create a typedef for the function pointer itself, just to save an awful lot of parens in the result.
    typedef int (*fnp)(char*);

    A simple cast would then be
    fnp myfun = (fnp)ptr;

    I'm sure you can work out the array bit from there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    Hey Saleem that was pretty nice trick. but i'm not so good with "typedef" and function pointers please show the answer for the above question in simple step so that i can learn from u....

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That was the example for you to learn from.

    But since you spent an absolute maximum of 11 minutes thinking about it (probably a lot less), I think I'll pass.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    ok. so for the above question is the following cast proper
    Code:
    (int ( (*)[3]) (char *) ) ptr

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You cannot assign function pointers to void pointers. No matter how you cast them.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    void pointers are always defined for flexibility we can cast void pointers to anything....

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No you can't. I'm right. You're wrong. Search the forum, I've proven this point before.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by c99
    J.5 Common extensions
    1 The following extensions are widely used in many systems, but are not portable to all
    implementations.
    ...
    J.5.7 Function pointer casts
    1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to
    be invoked as a function (6.5.4).
    2 A pointer to a function may be cast to a pointer to an object or to void, allowing a
    function to be inspected or modified (for example, by a debugger) (6.5.4).
    > void pointers are always defined for flexibility
    Yes, but not all pointers are the same size - the C standard doesn't say they have to be, and some very common architectures have had different sized pointers. void* only guarantees to be able to represent conversions to/from data pointers.
    Eg. In DOS 'medium' memory model, all function pointers would be 32-bit and void* would only be 16-bit.

    If you can do it, then put it in the "works for me" column, but one day you may have a surprise.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    Probably the solution is getting deviated..... can below be a solution for the problem
    Code:
    (int ( (*)[3]) (char *) ) ptr

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM