Thread: a pointer to a function question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    a pointer to a function question..

    Code:
    #include <stdio.h>
    
    int add(int x, int y);  /* declare function */
    
    int main() {
      int x=6, y=9;
      int (*ptr)(int, int); /* declare pointer to function*/
    
      ptr = add; /* set pointer to point to "add" function */
    
      printf("%d plus %d equals %d.\n", x, y, (*ptr)(x,y));
      /* call function using pointer */ 
      return 0;
    }
    
    int add(int x, int y) { /* function definition */
     return x+y;
    }
    in this line
    Code:
    int (*ptr)(int, int);
    i can see that they are building an int type pointer but i can see here the name
    of the function "add" which we are using ,plus this makes no sense (int, int);
    usually when we build a function we write some names to the input integers like
    add(int x,int y)


    in this line:
    Code:
    ptr = add;
    they treat add as if it was a simple variable and the is no &

    i think it should look like
    Code:
    *ptr = &add(int x,int y);

    ??

  2. #2
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    and in this line
    Code:
    printf("&#37;d plus %d equals %d.\n", x, y, (*ptr)(x,y));
    they are performing some sort of casting (*ptr)(x,y)
    to type pointer
    why arent the writing just *ptr

    ??

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't use parameter names in a type declaration like this -- you can obviously in a prototype, but not when declaring a pointer.

    Just like an array, a function name decays to a pointer when used by itself.

    And since function-call parentheses bind tighter than *, if you tried *ptr(x,y), the compiler would think ptr itself is a function, and then use the return result and dereference the *. Since we need to go the other way around (dereference the * first to get a function, then call it), we need the parentheses to change the order of operations.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    i think it should look like
    Code:
    ptr = &add;
    is perfectly valid to get the address of a function. Since functions do not have a value, some smart cookie defining the language decided that "why not just let the function name represent the address of the function always, no matter how many (including zero) & there is in front of the function name when it is used without parenthesis [which makes it a call to the function]". This is similar to the way that an array most of the times turns into the address of the first element.

    So
    Code:
    ptr = add;
    and
    Code:
    ptr = &add;
    is the exact same thing. As is the more bizarre form:
    Code:
    &*ptr = &&&&&&&add;


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by matsp
    is the exact same thing. As is the more bizarre form:
    I do not think so since the result of &*ptr is not an lvalue and due to greedy matching && will be interpreted as a single operator.
    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

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    I do not think so since the result of &*ptr is not an lvalue and due to greedy matching && will be interpreted as a single operator.
    Ok, not valid. But no sane person would write that anyways, so I guess it doesn't matter...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Here is the true judge of character here: Did laser try to compile it, or just recognized the invalid lvalue?

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok so first they make a variable ptr that will fit the input and output of
    the function add
    Code:
    int (*ptr)(int, int);
    then they link i to the function add

    Code:
    ptr = add; /* set pointer to point to "add" function */
    i got a problem in understand this second step
    because when i link a simple variable to a pointer i do

    Code:
    int *ptr = &x; //we can also write it as
    
    int *ptr;
     *ptr=&x //assign the data on address x to the pointer ptr 
                  // there has to be a *
    but when i see the linking to a function code line
    its so different

    ??

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by transgalactic2 View Post
    i can see that they are building an int type pointer but i can see here the name
    of the function "add" which we are using ,plus this makes no sense (int, int);
    usually when we build a function we write some names to the input integers like
    add(int x,int y)
    It makes perfect sense.
    It says that it's a pointer to a function that returns int, and takes one parameter of type int and a second parameter of type int.
    So there you have it - all of the information baked into the function pointer syntax.

    in this line:
    Code:
    ptr = add;
    they treat add as if it was a simple variable and the is no &
    Well, as mats have implied, it's legal to take the address of a function without the &, but I wouldn't really recommend it.

    i think it should look like
    Code:
    *ptr = &add(int x,int y);
    1) We are assigning an address to the pointer and we can't do that by dereferencing it! Plus the value inside the pointer is as of yet undefined, yet another reason not to dereference it! What were you thinking!?
    2) Just like with everything else, you use the name of the function, and do not include the parameters, because that's a declaration.

    Oh and it's also possible to invoke a function pointer just as if it were a function.
    Example:
    Code:
    int (*ptr)(int, int) = &add;
    (*ptr)(1, 1); // Method 1
    ptr(1, 1); // Method 2. My preferred way.
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by master5001
    Did laser try to compile it, or just recognized the invalid lvalue?
    I saw that an rvalue was on the left hand side of an assignment, compiled to check... and it seems that the compiler insisted that it was an "invalid lvalue", as you say. But C99 agrees with me by stating that the result is not an lvalue (so it must be an rvalue, right?)

    On the other hand, I did not catch the greedy matching of && until after my compile test.
    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

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The greedy matching is one of those things I know you had to notice on your own. How many compilers notice those things? Usually it informs you of the more pressing issue of the rvalue first. So I guess both would be a satisfactory answer?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by master5001
    The greedy matching is one of those things I know you had to notice on your own. How many compilers notice those things?
    They might not notice, but once they flag the error, the reason is obvious.

    Okay, maybe not, since I have seen people get completely puzzled trying to figure out why std::vector<std::vector<int>> does not work. But we digress.
    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

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    why only ptr=add;

    Code:
    why not ptr=add(int x,int y);  //add is a function we cant hide its signature 
                                                //what if there were two add function but with different signatures
                                                //in such case this line will create an error
    ??

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't have two functions with the same name.

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so it can be both
    ptr=add;

    and

    ptr=&add;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  3. Function pointer question
    By sbayeta in forum C Programming
    Replies: 9
    Last Post: 08-06-2004, 08:15 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM