473,616 Members | 2,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning a function pointer

What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(

Aug 4 '07 #1
11 1944
Antoninus Twink wrote:
What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(
I have never managed to do it without a typedef...

char *fn1(int a)
{
return "function 1";
}

char *fn2(int a)
{
return "function 2";
}

typedef char *(*FunctionType )(int);
FunctionType FunctionReturni ngAFunctionPoin ter(int a)
{
if (a 0)
return fn1;
else
return fn2;
}
Aug 4 '07 #2
Antoninus Twink said:
What's the correct syntax to define a function that returns a pointer
to a function? Specifically, I'd like a function that takes an int,
and returns a pointer to a function that takes an int and returns a
string.
char *foo(int x)
{
static char bar[2];
bar[0] = x;
return bar;
}

char *(*baz(int n))(int)
{
/* use n in some way, I guess */

return foo;
}

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 4 '07 #3
Antoninus Twink wrote:
>
What's the correct syntax to define a function that returns a
pointer to a function? Specifically, I'd like a function that
takes an int, and returns a pointer to a function that takes an
int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(
How about:

typedef char *stringfromint( int);
stringfromint *transfer(int) {
... amazing code ...
};

--
"Vista is finally secure from hacking. No one is going to 'hack'
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."
--
Posted via a free Usenet account from http://www.teranews.com

Aug 4 '07 #4
Antoninus Twink <sp*****@invali d.comwrites:
What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(
Could the tool cundecl help you?
Aug 4 '07 #5
The function you have declared was right. I have a programme using it like
this:

#include <stdio.h>
char * (* func (int b)) (int); //The function returns a pointer.
char fun_1 (int a); //The function that the pointer pointed.
int main (void)
{
int b;
char ch;
char (*pre) (int a); //Declare an pointer point to func_1.

b = 98;

pre = (func (b)); //Get the pointer returned.
ch = (*pre) (b); //"pre" pointed to fun_1 ().
printf ( "ch = %c\n", ch);

return 0;
}
char func_1 (int a)
{
return a;
}
char * (* func (int b)) (int)
{
char (*pre) (int a);
if (b == 98)
pre = func_1;
return pre; //Return the pointer we want.
}

Exegesis: Visual C++ 6.0
"Antoninus Twink" <sp*****@invali d.comдÈëÏûϢРÎÅ:sl********** **********@nosp am.invalid...
What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(

Aug 5 '07 #6
Antoninus Twink wrote:
What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(
How about this?

#include <stdio.h>

typedef char*(*fp_t)(in t);

char glob[20];

char *foo(int i) {
sprintf(glob, "You've called foo with %d\n", i);
return glob;
}

char *bar(int i) {
sprintf(glob, "You've called bar with %d\n", i);
return glob;
}

fp_t baz(int i) {
fp_t ret;
if (i)
ret = foo;
else
ret = bar;
return ret;
}

int main(void) {
fp_t fun;
fun = baz(1);
puts(fun(42));
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 5 '07 #7
On Sun, 5 Aug 2007 17:06:58 +0800, "Colonel" <xi**********@1 63.com>
wrote:
>The function you have declared was right. I have a programme using it like
this:

#include <stdio.h>
If this is your actual code, you need to up the warning level of your
compiler and pay heed to the diagnostics.
>char * (* func (int b)) (int); //The function returns a pointer.
The pointer that func returns points to a function. That function
returns a char*.
>char fun_1 (int a); //The function that the pointer pointed.
fun_1 returns a char. Its address cannot be the return value from
func.
>int main (void)
{
int b;
char ch;
char (*pre) (int a); //Declare an pointer point to func_1.
pre is a pointer to function that returns a char. It is compatible
with fun_1.
>
b = 98;

pre = (func (b)); //Get the pointer returned.
func returned a pointer to a function that returns a char*. It is
**not** compatible with pre.
ch = (*pre) (b); //"pre" pointed to fun_1 ().
printf ( "ch = %c\n", ch);

return 0;
}
char func_1 (int a)
{
return a;
}
char * (* func (int b)) (int)
Again, the pointer that func returns points to a function returning a
char*. (This is compatible with the prototype above.)
>{
char (*pre) (int a);
Again, this local pre points to a function that returns char. It is
not compatible with the return type of func.
if (b == 98)
pre = func_1;
This is a constraint violation. The two operands of the assignment
operator are incompatible. There is no implicit conversion between
the two.
return pre; //Return the pointer we want.
It may be the pointer you want but it is the wrong type to return from
this function.
>}

Exegesis: Visual C++ 6.0
"Antoninus Twink" <sp*****@invali d.comдÈëÏûϢРÎÅ:sl********** **********@nosp am.invalid...
>What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */
}

but this doesn't seem to work.

:(

Remove del for email
Aug 5 '07 #8
On Aug 4, 5:10 pm, Antoninus Twink <spam...@invali d.comwrote:
What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */

}

but this doesn't seem to work.

:(
Define "doesn't seem to work." Are you getting a syntax error? A
runtime error? What?

f -- f
f() -- is a function
f(int n) -- that takes an integer
*f(int n) -- and returns a pointer
(*f(int n))() -- to a function
(*f(int n))(int m) -- that takes an integer
char *(*f(int n))(int m) -- and returns a char *

So, apart from the gint/gchar weirdness (I'm guessing this comes from
some API you're using), your definition looks all right to me.

char *foo(int m)
{
/* does something interesting */
}

char *bar(int m)
{
/* does something interesting */
}

char *bletch(int m)
{
/* does something interesting */
}

char *(*f(int n))(int m)
{
char *(*p)(int m);

switch(n)
{
case 0: p = foo; break;
case 1: p = bar; break;
case 2: p = bletch; break;
default: p = NULL; break;
}

return p;
}

int main(void)
{
char *result;
char *(*p)(int m);

int i;

for (i = 0; i < 4; i++)
{
p = f(i);
result = p(123);
if (result)
{
printf("result = %s\n", result);
}
}

return 0;
}

Aug 6 '07 #9
On Mon, 06 Aug 2007 14:45:01 -0700, John Bode wrote:
On Aug 4, 5:10 pm, Antoninus Twink <spam...@invali d.comwrote:
>What's the correct syntax to define a function that returns a pointer to
a function? Specifically, I'd like a function that takes an int, and
returns a pointer to a function that takes an int and returns a string.

I tried this:

gchar *(*f(gint n))(gint)
{
/* logic here */

}

but this doesn't seem to work.

:(

Define "doesn't seem to work." Are you getting a syntax error? A
runtime error? What?

f -- f
f() -- is a function
f(int n) -- that takes an integer
*f(int n) -- and returns a pointer
(*f(int n))() -- to a function
(*f(int n))(int m) -- that takes an integer
char *(*f(int n))(int m) -- and returns a char *

So, apart from the gint/gchar weirdness (I'm guessing this comes from
some API you're using), your definition looks all right to me.

char *foo(int m)
{
/* does something interesting */
}

char *bar(int m)
{
/* does something interesting */
}

char *bletch(int m)
{
/* does something interesting */
}

char *(*f(int n))(int m)
{
char *(*p)(int m);

switch(n)
{
case 0: p = foo; break;
case 1: p = bar; break;
case 2: p = bletch; break;
default: p = NULL; break;
}

return p;
}

int main(void)
{
char *result;
char *(*p)(int m);

int i;

for (i = 0; i < 4; i++)
{
p = f(i);
result = p(123);
What happens the fourth time the loop body is executed? :-)
if (result)
{
printf("result = %s\n", result);
}
}

return 0;
}
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3081
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
11
1882
by: Justin Naidl | last post by:
class Foo { protected: char foo_stuff; public: char* get_foo_stuff(); } Given the above example. What I want to know is the "proper/standard" way
41
3794
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x = rand(); y = rand();
10
3153
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
3
1846
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is found this is done if funktion serach_char. so far all good what I want do next is: return, from funktion, pointer value to array were positions ( of found char) is stored. and print that array from main. but I only manage to print memory adress to...
17
3240
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
23
2922
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or method. QUOTE ENDS HERE I have never heard anyone else say that it is a problem for a function
8
2209
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
5
2674
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
8146
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8297
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6097
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4063
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2579
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.