Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Returning pointer to array of function pointers not working

4: Adsense

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   

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

#2 Tribad   Members   

981
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   

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   Moderators   

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

zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#5 King Mir   Members   

2490
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   

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   

225
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   

981
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   

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


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




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.