Returning pointer to array of function pointers not working

Started by
7 comments, last by ApochPiQ 9 years, 10 months ago

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.

[size="5"]http://innercirclegames.freeforums.org
Email me at: innercirclegames@hotmail.com
Advertisement

Would be more readable, even for the compiler, if you create a typedef for the function.


#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.

[size="5"]http://innercirclegames.freeforums.org
Email me at: innercirclegames@hotmail.com
Also, you cannot use the pointers returned from a, b, and c. The z that ptr points to expires once the function ends, and reading from or writing to that pointer has undefined behavior.

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.

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();
}
[size="5"]http://innercirclegames.freeforums.org
Email me at: innercirclegames@hotmail.com

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.

I use an array of pointers to pointers to functions. If the array shall be dynamic in size you have another 'pointer to' indirection

The use of & in your code is not a reference, it's taking an address of something.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement