Thread: Tough problem with function pointers

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

    Tough problem with function pointers

    Hi everyone,
    I'm working on a program for numericaly solving differential equations, I have an index i which runs from -LATICE to LATICE. This index undergoes a coordinate substitution according to the problem and is mapped to [0, infinity) in radial coordinates, (-infinity,infinity) for one dimensional problems or [-N,N] for limited one-dimensional problems. I use a function pointer to choose which of the mapping function will be used during run-time.
    The problem comes with the third mapping, the scaling [-N,N] is just i*N but N is also determined during run-time. So, is there a way of defining a function which is given as parameter N and returns a pointer to a function which multiplies i with N?

    I tried a bit or round-working, I made a function with variable number of arguments, if more than one parameter is given, the second one sets a static variable N. The first parameter is just i and it returns i*N. And to make it compatible with the function pointer (which needs fixed number of arguments) it is called once to set N and then I use a function with just calls this one without second parameter. So, kind of ugly solution.

    Anyone knows a solution to the actual problem?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Yep! you can have an array of pointers to functions, each returning a pointer to a function. For the the exact solution it would help to show some code.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    2
    Quote Originally Posted by itCbitC View Post
    Yep! you can have an array of pointers to functions, each returning a pointer to a function. For the the exact solution it would help to show some code.
    The problem is each function in the array should be multiplication with N (varying with the index) but
    a) N is not integer but double
    b) even if integer that would mean writing infinite multiplication functions for each N

    Thanks anyway

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Montejo View Post
    The problem is each function in the array should be multiplication with N (varying with the index) but
    a) N is not integer but double
    How is that a problem?
    Quote Originally Posted by Montejo View Post
    b) even if integer that would mean writing infinite multiplication functions for each N
    Nothing in your problem description indicates this to be the case. It might help for you to post some code.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Montejo View Post
    The problem is each function in the array should be multiplication with N (varying with the index) but
    a) N is not integer but double
    b) even if integer that would mean writing infinite multiplication functions for each N

    Thanks anyway
    Totally confused with what you'ave said above. Post some code and someone might be able to help you out.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Montejo View Post
    So, is there a way of defining a function which is given as parameter N and returns a pointer to a function which multiplies i with N?
    This is easier in C++.

    I reccomend avoiding functions with variable number of arguments.

    If you only need one possible value N at a time you could use a global or static variable. If you have a finite list of possible values of N, then you can have multiple functions for each value of N. Macros can make defining them not take so much code.

    Otherwise there is no way to avoid being ugly. You basically need the function that is returned to have an extra data parameter to store N. Since you seems to be storing it in a pointer that can also point to functions that don't have data, those functions will still need the extra unused parameter. For symmetry you can make it so that the functions that generate the other types of mapping also return extra useless data.

    Effectively you'd be implementing OOP in C. So if you can, use C++, which implements OOP for you.

    For those that don't understand the question, the OP wants to do this:
    Code:
    //C++0x code
    
    int (*func(N))(int){
      return [=](int i)->{return i*N;};
    }
    @Montejo
    Real C++ isn't quite this easy, but it is easier than C.
    Last edited by King Mir; 12-21-2009 at 05:13 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by King Mir
    Effectively you'd be implementing OOP in C.
    No, functional programming, not OOP. I actually tried to research how to use functional programming in C when I read that, but the answers to be found online appeared too complicated to bother if Montejo's problem can be solved differently, hence I refrained from a reply.
    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

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    EDIT: I guess that's true. But it can be solved with OOP.
    Last edited by King Mir; 12-21-2009 at 10:40 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by King Mir
    It can be done with a lambda, or it can be done with functors or it can be done with objects and methods. Lambda's are functional constructs, objects are OOP. Functors are in between. So either functional programming or OOP will do.
    The "objects and methods" option that you talk about is just a way to simulate a functor by designating a named method as a callback, even though this does not allow the object to be "called" like a function. I do not agree with the identification of the use of objects as evidence of OOP per se. Sure, if there is an inheritance hierarchy of function objects, then it is obvious that OOP is involved, but here I feel that the intent to return a function from a function and apply the function returned is more on the lines of functional programming. So, the use of a function object here is effectively simple functional programming.
    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

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yeah, I changed my mind after I wrote that.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Montejo View Post
    So, is there a way of defining a function which is given as parameter N and returns a pointer to a function which multiplies i with N?
    That's the only part I get because the next paragraph obfuscates it all; and the above can be implemented as
    Code:
    int (*(f(int N))) (int);   /* prototype of function returning pointer to function that takes and returns an int */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  3. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-17-2005, 12:55 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 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