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 onedimensional problems. I use a function pointer to choose which of the mapping function will be used during runtime. The problem comes with the third mapping, the scaling [N,N] is just i*N but N is also determined during runtime. 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 roundworking, 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? 
Re: Tough problem with function pointers

Re: Tough problem with function pointers
I did, but if I'm not wrong that's only a feature of OOlanguages, and I'm using C
Sorry I didn't say it explicitly, I'm editing the original post. So, a way of doing it without functors? Or can they be implemented anyhow in C? 
Re: Tough problem with function pointers
No, there's no operator overloading in C so the functor concept will not be achievable.
I don't know for sure what the problem is from your description. You said you're using a variable parameter list so that will necessarily compile with cdecl calling conventions and should be OK but I don't see how it is you are determining whether or not there are extra parameters in the list. You can end up with a messed up stack if you do it wrong. Is there some reason why you can't use a dummy parameter in the functions that don't need the extra parameter and just ignore it? 
Re: Tough problem with function pointers
Quote:
I mean, I've got a solution, not nice but it works. It was more of an educational question. I really would like to learn how to do this (if it is possible) with function pointers. Thanks a lot anyway 
Re: Tough problem with function pointers
Well, as long as the functions are declared __cdecl, you should be safe just doing a static cast on the function pointer to use as many parameters as you want. Something like this:
Code:
#include <stdio.h> 
Re: Tough problem with function pointers
Problem is that it's not posible to write a diferent function that does multiplication with every available N.
What I was asking for is a function which is given N as parameter and returns a pointer to a function which does multiplication with N. 
Re: Tough problem with function pointers
Oh, so your just wanting a variable parameter function? Something like this
Code:

All times are GMT +5.5. The time now is 07:26. 