Thread: Function Pointers - What does this block do?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Function Pointers - What does this block do?

    Hey. I've been researching into operating system programming.
    I am trying to figure out what exactly what each line of code does.

    For example, the function below copies chunks of memory from a source to a destination. In order for me to understand how the code works, It is necessary to decipher each line individually.

    Code:
    unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)
    {
        const char *sp = (const char *)src;
        char *dp = (char *)dest;
        for(; count != 0; count--) *dp++ = *sp++;
        return dest;
    }
    From what I know already, the following code takes the source's addresses in memory, and maps it onto the destination memory address, thereby copying it.
    Code:
     for(; count != 0; count--) *dp++ = *sp++;
     return dest;
    However I do not understand some things.

    Firstly,
    Code:
     char *dp = (char *)dest;
    I've never seen anything like the above code. What I do not understand is what the parenthesis does. Common sense would dictate that this is simply assigning the destination to a pointer, however as I do not understand what the purpose of the parentheses is, I cannot fully understand the purpose of this code. Could someone kindly explain this to me?

    The other thing I do not understand is at the declaration of the function, the name of the function has a * before it. I do not understand what this does. Does it return the pointer of the return value instead of the actual return value?

    Thanks in Advance,


    ~ShadowzReloaded
    Last edited by shadowzreloaded; 04-09-2010 at 12:03 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by shadowzreloaded View Post
    I've never seen anything like the above code. What I do not understand is what the parenthesis does. Common sense would dictate that this is simply assigning the destination to a pointer, however as I do not understand what the purpose of the parentheses is, I cannot fully understand the purpose of this code. Could someone kindly explain this to me?

    The other thing I do not understand is at the declaration of the function, the name of the function has a * before it. I do not understand what this does. Does it return the pointer of the return value instead of the actual return value?

    Thanks in Advance,


    ~ShadowzReloaded
    No offense, but rather than have us teach you the basics of C here, you should probably be studying that sort of thing on your own. I'd recommend reading at least two books on the subject before you start asking questions, actually.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Please post something constructive. I never usually post questions. But this is an exception, I was unable to find anything on this subject, also, since when were function pointers basic C?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There are no function pointers anywhere in this code. The fact that you think so, is the problem.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Yeah, you're right. Sorry about that. What I really meant was a pointer to a function, not a function pointer. I'm sorry.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shadowzreloaded
    I've never seen anything like the above code. What I do not understand is what the parenthesis does. Common sense would dictate that this is simply assigning the destination to a pointer, however as I do not understand what the purpose of the parentheses is, I cannot fully understand the purpose of this code. Could someone kindly explain this to me?
    Search the Web for "C typecast".

    Quote Originally Posted by shadowzreloaded
    The other thing I do not understand is at the declaration of the function, the name of the function has a * before it. I do not understand what this does. Does it return the pointer of the return value instead of the actual return value?
    The return type of the function is a pointer type.

    Quote Originally Posted by shadowzreloaded
    What I really meant was a pointer to a function, not a function pointer.
    The terms are synonymous.
    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

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by shadowzreloaded View Post
    Yeah, you're right. Sorry about that. What I really meant was a pointer to a function, not a function pointer. I'm sorry.
    A function pointer is a pointer to a function. You don't have that either.

    You have a function, and functions all work in the same way:
    Code:
    function_return_type name_of_function(type_of_param param_1, <etc>)
    Your function follows this form exactly.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Wow. Thanks for your quick reply. And finally something constructive .

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by shadowzreloaded View Post
    Please post something constructive. I never usually post questions. But this is an exception, I was unable to find anything on this subject, also, since when were function pointers basic C?
    Nothing you posted had anything to do with function pointers, actually.
    So, you see, when you don't even know the basics of the language, it just makes it difficult for all parties involved. Do your part, for everyone's sake. Pretty please.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    @tabstop
    Code:
    unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)
    My function name is a pointer, so it is a variation of a function pointer.(more specifically a pointer returning function)
    It would follow this structure?

    Code:
    function_return_type *name_of_function(type_of_param param_1, <etc>)
    Anyway that is what is said at this page:
    The C Book — Pointers to functions

    Sorry for any inconvenience caused if I am wrong.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shadowzreloaded
    My function name is a pointer, so it is a variation of a function pointer.
    It is true that you do not need to use the address-of operator in order to obtain the address of a function, i.e., a function pointer. Basically, a function is convertible to a function pointer. But a function is not a function pointer.
    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

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    I see. Thank you for clarifying that for me. A function pointer itself however would have parenthesis around the *name of the function?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shadowzreloaded
    A function pointer itself however would have parenthesis around the *name of the function?
    No, but you are probably talking about the name of the function pointer, not the name of the function.
    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

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Yeah thats what I meant. Thank you

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by shadowzreloaded View Post
    I see. Thank you for clarifying that for me.
    Go to the library and get a basic "Learn C in 7 days" book, or do some basic C tutorials on line. You are totally wasting your time trying to decypher things this way when you don't understand even very basic syntax such as this (that a function returning a pointer means there is a pointer to that function).

    I am not saying that to be mean, I am saying it because it is true. If you are trying to learn C, doing it by "researching into operating system programming" is just plain stupid (sorry). If you do not understand C, which you do not, then "researching into operating system programming" is not going to go very well for you.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread