# Tough problem with function pointers

Discussion in 'C++' started by Montejo, Dec 21, 2009.

1. ### MontejoNew Member

Joined:
Dec 21, 2009
Messages:
4
0
Trophy Points:
0
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. ### Gene PooleNew Member

Joined:
Nov 10, 2009
Messages:
93
5
Trophy Points:
0
3. ### MontejoNew Member

Joined:
Dec 21, 2009
Messages:
4
0
Trophy Points:
0
I did, but if I'm not wrong that's only a feature of OO-languages, 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?

4. ### Gene PooleNew Member

Joined:
Nov 10, 2009
Messages:
93
5
Trophy Points:
0
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?

5. ### MontejoNew Member

Joined:
Dec 21, 2009
Messages:
4
0
Trophy Points:
0
No inevitable reason apart from clean code, I use the mapping function a zillion times in the code and I don't like passing extra parameters which aren't being used.

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

6. ### Gene PooleNew Member

Joined:
Nov 10, 2009
Messages:
93
5
Trophy Points:
0
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>

int __cdecl func1(int a,int b)
{
printf("%d %d\n",a,b);
return 1;
}

int __cdecl func2(int a,int b,int c)
{
printf("%d %d %d\n",a,b,c);
return 1;
}

int __cdecl func3(int a,int b,int c,int d)
{
printf("%d %d %d %d\n",a,b,c,d);
return 1;
}

typedef int (*ptr1)(int,int);
typedef int (*ptr2)(int,int,int);
typedef int (*ptr3)(int,int,int,int);

int main(void) {

int i;

i=((ptr1)(&func3))(1,2);
i=((ptr2)(&func2))(1,2,3);
i=((ptr3)(&func1))(1,2,3,4);
return 0;

}
```
should be perfectly safe.

7. ### MontejoNew Member

Joined:
Dec 21, 2009
Messages:
4
0
Trophy Points:
0
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.

8. ### Gene PooleNew Member

Joined:
Nov 10, 2009
Messages:
93
5
Trophy Points:
0
Oh, so your just wanting a variable parameter function? Something like this

Code:
```
#include <stdio.h>
#include <stdarg.h>
#include <varargs.h>

double MultFunc(int n,...)
{
int i;
double d=0.0,tmp;

va_list ap;
va_start(ap,n);
d=va_arg(ap,double);
for(i=0;i<(n-1);i++){
tmp=va_arg(ap,double);
d*=tmp;
}
va_end(ap);
return d;
}

int main(void)
{
double d;

//first argument tells how many doubles follow
d=MultFunc(5,21.005,56.12,2.1313,15.1181,7.7);
return 0;

}
```