Sign in to follow this  

Returning pointer to array of function pointers not working

This topic is 1279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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

This topic is 1279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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