473,386 Members | 1,598 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,386 software developers and data experts.

Function returning pointer to itself

typedef void (*vfp)();
typedef vfp (*fp)();

static fp hello()
{
printf("Hello.\n");
return (fp)&hello;
}

main(){
hello()()();
}

Here we have a function that returns a fp, which is a function pointer
typedef, the function of which returns a vfp, which is another function
pointer typedef but which returns void, so we can use hello()()(); but
not hello()()()(); unless we have a cast. I was wondering if there's a
way to do this properly with some kind of properly recursive typedef. I
saw an example of how to do this fairly "nicely" in C++ but couldn't
find anything for C itself. Anyone here know how to do this? Purely
theoretical by the way, wouldn't want to release this on some poor,
unsuspecting, real project.

Cheers,
John
Jan 23 '07 #1
3 3641
John Turner wrote:
typedef void (*vfp)();
typedef vfp (*fp)();

static fp hello()
{
printf("Hello.\n");
return (fp)&hello;
}

main(){
hello()()();
}
This is not valid C. hello() is declared as returning 'fp', and you
call it through a pointer to a function returning 'vfp'. As far as
standard C is concerned, the behaviour is undefined. There are other
problems with the code too: you're calling printf without including
<stdio.h>, and you're using implicit int and unprototyped functions.
The first was never valid, the second is no longer valid, the third is
a bad idea as it makes it difficult for compilers to check the argument
types for you.
Here we have a function that returns a fp, which is a function pointer
typedef, the function of which returns a vfp, which is another function
pointer typedef but which returns void, so we can use hello()()(); but
not hello()()()(); unless we have a cast. I was wondering if there's a
way to do this properly with some kind of properly recursive typedef.
This is a FAQ:
http://c-faq.com/decl/recurfuncp.html

#include <stdio.h>
struct fp {
struct fp (*fp)(void);
};
struct fp hello(void) {
struct fp ret = { hello };
printf("Hello.\n");
return ret;
}
int main(void) {
hello().fp().fp().fp().fp().fp().fp();
return 0;
}

Jan 23 '07 #2
Rg
On 23 jan, 11:04, "Harald van Dijk" <true...@gmail.comwrote:
>
John Turner wrote:

typedef void (*vfp)();
typedef vfp (*fp)();
static fp hello()
{
printf("Hello.\n");
return (fp)&hello;
}
main(){
hello()()();
}

This is not valid C. hello() is declared as returning 'fp', and you
call it through a pointer to a function returning 'vfp'. As far as
standard C is concerned, the behaviour is undefined. [...]
An alternative explanation:

hello() evaluates to an expression of type fp, which is also vfp (*)()
or void (*(*)())()

hello()() evaluates to vfp, which is also void (*)()

hello()()() evaluates to void

Finally, hello()()()() leads to syntax error it is an attempt to
perform a function call over an object of type void.

Jan 23 '07 #3
Harald van Dijk wrote:
John Turner wrote:
>typedef void (*vfp)();
typedef vfp (*fp)();

static fp hello()
{
printf("Hello.\n");
return (fp)&hello;
}

main(){
hello()()();
}

This is not valid C. hello() is declared as returning 'fp', and you
call it through a pointer to a function returning 'vfp'. As far as
standard C is concerned, the behaviour is undefined. There are other
problems with the code too: you're calling printf without including
<stdio.h>, and you're using implicit int and unprototyped functions.
The first was never valid, the second is no longer valid, the third is
a bad idea as it makes it difficult for compilers to check the argument
types for you.

This is a FAQ:
http://c-faq.com/decl/recurfuncp.html

#include <stdio.h>
struct fp {
struct fp (*fp)(void);
};
struct fp hello(void) {
struct fp ret = { hello };
printf("Hello.\n");
return ret;
}
int main(void) {
hello().fp().fp().fp().fp().fp().fp();
return 0;
}
I'm sorry some of the C wasn't valid. I was aware of the hello()
declaration being fundamentally wrong and forgetting stdio.h and the
implicit int declaration of main was just right out. Thanks for
pointing me to the FAQ answer, I hadn't found that before (I've looked
through some of that FAQ, but missed that one...). I'd managed to
implement this using structs too, but was hoping to be able to use it
with the syntax of hello()()()()() just out of interest. It makes sense
to me that it's not possible now though.
Jan 23 '07 #4

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

Similar topics

3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
14
by: Mike Labosh | last post by:
How do you define whether the return value of a function is ByRef or ByVal? I have a utility class that cleans imported records by doing *really heavy* string manipulation in lots of different...
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
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: ...
9
by: key9 | last post by:
Hi all My question is why the code CAN pass the compiler , and all the resoult is right? I means in my mind the reffoo(3,*test); should not pass compiler. #include <stdio.h>
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.