Jump to content

  • Log In with Google      Sign In   
  • Create Account

Returning pointer to array of function pointers not working


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Amaz1ng   Members   -  Reputation: 131

Like
0Likes
Like

Posted 16 June 2014 - 06:39 AM

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, 16 June 2014 - 06:44 AM.

http://innercirclegames.freeforums.org
Email me at: innercirclegames@hotmail.com

Sponsor:

#2 Tribad   Members   -  Reputation: 851

Like
4Likes
Like

Posted 16 June 2014 - 06:44 AM

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



#3 Amaz1ng   Members   -  Reputation: 131

Like
0Likes
Like

Posted 16 June 2014 - 09:37 AM

#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, 16 June 2014 - 09:42 AM.

http://innercirclegames.freeforums.org
Email me at: innercirclegames@hotmail.com

#4 fastcall22   Crossbones+   -  Reputation: 4330

Like
2Likes
Like

Posted 16 June 2014 - 10:25 AM

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.

Edited by fastcall22, 16 June 2014 - 10:57 AM.

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#5 King Mir   Members   -  Reputation: 2004

Like
0Likes
Like

Posted 16 June 2014 - 08:50 PM

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.



#6 Amaz1ng   Members   -  Reputation: 131

Like
0Likes
Like

Posted 17 June 2014 - 05:14 AM

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, 17 June 2014 - 06:49 AM.

http://innercirclegames.freeforums.org
Email me at: innercirclegames@hotmail.com

#7 Nick C.   Members   -  Reputation: 190

Like
0Likes
Like

Posted 17 June 2014 - 09:46 AM

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.



#8 Tribad   Members   -  Reputation: 851

Like
0Likes
Like

Posted 17 June 2014 - 11:04 AM

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

#9 ApochPiQ   Moderators   -  Reputation: 15692

Like
0Likes
Like

Posted 17 June 2014 - 01:22 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS