473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1915
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 FunctionReturningAFunctionPointer(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*****@invalid.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*****@invalid.comдÈëÏûÏ¢ÐÂÎÅ:sl*************** *****@nospam.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)(int);

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**********@163.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*****@invalid.comдÈëÏûÏ¢ÐÂÎÅ:sl*************** *****@nospam.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...@invalid.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...@invalid.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
On Aug 6, 7:20 pm, Army1987 <army1...@NOSPAM.itwrote:
On Mon, 06 Aug 2007 14:45:01 -0700, John Bode wrote:
On Aug 4, 5:10 pm, Antoninus Twink <spam...@invalid.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? :-)
Something wonderful and unexpected.
>
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 #11
On Sun, 05 Aug 2007 10:23:50 -0400, Joe Wright
<jo********@comcast.netwrote:
<snip>
How about this?
<snip>
char glob[20];

char *foo(int i) {
sprintf(glob, "You've called foo with %d\n", i);
return glob;
}
<snip bar() similar and funcptr to one of them used>

glob needs to be 27 chars for the valued used (42), and should be
rather larger to be safe in general.

Even better use snprintf, standard in C99 and fairly common
before/without that, in case you do get it wrong.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Aug 26 '07 #12

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

Similar topics

5
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...
11
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
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...
10
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...
3
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...
17
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: ...
23
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...
8
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
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.