Sign in to follow this  
Amaz1ng

Returning pointer to array of function pointers not working

Recommended Posts

I tried this code and it seems to check out when i type it into cdecl.org but the function "dbl" gives me a compile error when i try to return a pointer to an array of function pointers. Am I defining "fp" right?   

#include <iostream>
#include <cassert>
using namespace std;

#define DEF(X) double * X(int y) { \
	double z = (double)3.55+y;	\
	double * ptr = &z; \
	cout << "inside func " #X << ". Returning a dbl ptr with *ptr value of " \
	<< *ptr << endl; return ptr;}

DEF(a); DEF(b); DEF(c);

/*
a pointer to an array of 3 pointers to functions that take an int and return a double
pointer
*/

double * (*fp[])(int) = {a,b,c};

/*
pointer to function that takes an int
and returns a pointer to a function that takes a double ptr and returns a pointer
to an array of 3 pointers to functions that take an int and return a double
pointer
*/

double * (*(*(*(*f1)(int))(double *))[3])(int);

 /*
a function that takes a double ptr and returns a pointer
to an array of 3 pointers to functions that take an int and return a double
pointer
 */

double * (*(*dbl(double *))[3])(int)
{
	cout << "inside dbl" << endl;
	return fp;
}

/*
function that takes an int
and returns a pointer to a function that takes a double ptr and returns a pointer
to an array of 3 pointers to functions that take an int and return a double
pointer
*/

double *(*(*(*func(int))(double *))[3])(int)
{
	cout << "inside func" << endl;
	return dbl;
}


int main()
{
	/*
	char choice  = 'c';
	double * ptr;
	ptr = (*fp[choice - 'a'])(5);
	assert(ptr != NULL);
	*/

	cin.get();
}

This is the error it gives lol.....

 

cannot convert from 'double *(__cdecl *[3])(int)' to 'double *(__cdecl *(*)[3])(int)

 

-edit- i dont think fp is actually a "pointer to an array of function pointers" but its just an "array of function pointers". But I don't know how to make it an actual pointer, I keep getting errors.

Edited by Amaz1ng

Share this post


Link to post
Share on other sites
#include <iostream>
#include <cstdlib>
using namespace std;


#define DEF(X) double * X(int y) { \
	double z = (double)3.55+y;	\
	double * ptr = &z; \
	cout << "inside func " #X << ". Returning a dbl ptr with *ptr value of " \
	<< *ptr << endl; return ptr;}

DEF(a); DEF(b); DEF(c);

typedef double * (*fp)(int);
fp arrf[] = {a,b,c};
fp * fptr = arrf;

fp * func(int x)
{
	cout << "inside func." << endl;
	return fptr;
}

int main()
{
	fp * lol;
	lol = func(5);

	*lol[0](3);
	*lol[1](4);
	*lol[2](5);

	cin.get();
}

i guess that works lol. I don't know what i was trying to do in the 1st post. Thanks for the typedef hint tho.

Edited by Amaz1ng

Share this post


Link to post
Share on other sites
Also, you cannot use the pointers returned from a, b, and c. The [tt]z[/tt] that [tt]ptr[/tt] points to expires once the function ends, and reading from or writing to that pointer has undefined behavior. Edited by fastcall22

Share this post


Link to post
Share on other sites

If you want to return an array in C++, make it an std::array. Old style C/C++03 array can not be returned by value, and returning a global harms code reuse, among other problems.

Share this post


Link to post
Share on other sites

I got dbl to work by simply passing "fp" as a reference (i think) like this &(fp).  such a simple thing   =/   I guess i can move on from function ptrs now

#include <iostream>
#include <cassert>
using namespace std;

#define DEF(X) double * X(int y) { \
	double z = (double)3.55+y;	\
	double * ptr = &z; \
	cout << "inside func " #X << ". Returning a dbl ptr with *ptr value of " \
	<< *ptr << endl; return ptr;}

DEF(a); DEF(b); DEF(c);

/*
a pointer to an array of 3 pointers to functions that take an int and return a double
pointer
*/

double * (*fp[])(int) = {a,b,c};

/*
pointer to function that takes an int
and returns a pointer to a function that takes a double ptr and returns a pointer
to an array of 3 pointers to functions that take an int and return a double
pointer
*/

double * (*(*(*(*f1)(int))(double *))[3])(int);

 /*
a function that takes a double ptr and returns a pointer
to an array of 3 pointers to functions that take an int and return a double
pointer
 */


double * (*(*dbl(double *))[3])(int)
{
	cout << "inside dbl" << endl;
	return &(fp);
}

/*
function that takes an int
and returns a pointer to a function that takes a double ptr and returns a pointer
to an array of 3 pointers to functions that take an int and return a double
pointer
*/


double *(*(*(*func(int))(double *))[3])(int)
{
	cout << "inside func" << endl;
	return dbl;
}



int main()
{
	/*
	char choice  = 'c';
	double * ptr;
	ptr = (*fp[choice - 'a'])(5);
	assert(ptr != NULL);
	*/
	
	f1 = func;
	double * (*(*(*adbl)(double *))[3])(int); //pointer to store the pointer to function from f1
	adbl = f1(3);
	double * x = NULL;
	adbl(x);

	cin.get();
}
Edited by Amaz1ng

Share this post


Link to post
Share on other sites

I have the feeling you're abusing pointers a bit :).
I've never in my life seen a pointer to pointer to pointer to pointer.

Share this post


Link to post
Share on other sites
I use an array of pointers to pointers to functions. If the array shall be dynamic in size you have another 'pointer to' indirection

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this