Thread: About pointer to function (Soved by myself)

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    Talking About pointer to function (Soved by myself)

    ----I love the pointer in C very well. I have just learned how to declare a pointer to a function and how to use it:
    Code:
        void (*start)(void);    /*The star should be placed with the word*/
        start = &hello;
        (*start)();
    ----But I'm not willing to use the pointer with a lot of different types. I'm wondering is it possible to transform a pointer between a void pointer and a pointer to function. This is strange, I can't figure out how to write it.

    ----And this is my solution:
    Code:
        (*                      /*Get the value of the function pointer */
            (void (*)(void))
    /*A function pointer type*/
            start               /*which is transformed from the void pointer to the type above.*/
        )
        ();                     /*Excute*/
    ----Oh my dear, it works, how amazing is that, and by the way I got the answer myself. Thanks God and also thank you everybody for sharing it with me.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is the point of thanking something that doesn't exist?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hchingwong
    I'm wondering is it possible to transform a pointer between a void pointer and a pointer to function.
    "A pointer to void may be converted to or from a pointer to any incomplete or object type." As such, I would say no, at least not in general.

    Quote Originally Posted by hchingwong
    And this is my solution: (...) Oh my dear, it works
    I think that you are misleading yourself. It looks like you are casting start to be a pointer to a function that takes no argument and returns void... but that is the type of start to begin with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Yah, I'm actually want to declare a void pointer and cast it to a function pointer.
    This one
    Code:
        void *start
    instead of this one
    Code:
        void (*start)(void);
    It works. But I know now this is some kind of bad thing. Maybe I should save the pointer with long long and then cast it to whatever type. This is just a joke. I now uncomfortable with it. Thank you laserlight and thank you Elysia for reading my post.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    and ';'. Another mistake.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot create a void datatype. You must create the appropriate type, eg function pointer, then store it in void*. You can later cast it back into its original type. This is usually how generic programming is done in C.
    Don't save pointers in non-void pointers. Different pointers can vary in size. Therefore, it is only guaranteed that you can cast something to void* and back. Not between S* and T* and back.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM