473,399 Members | 3,106 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,399 software developers and data experts.

A function that returns a pointer on a function

Hi,

I need to write a function that returns a pointer on a function. Is it
possible, and which is the syntax ?

Thanks.
Boris

Jul 22 '05 #1
3 1125

"Boris Sargos" <bs*****@wanadoo.fr> wrote in message
news:c6**********@news-reader4.wanadoo.fr...
Hi,

I need to write a function that returns a pointer on a function. Is it
possible, and which is the syntax ?

Thanks.
Boris


Trying to do that without using a typedef is a real test of your
understanding of type declarations. The easy way is with a typedef

typedef void (*FUNC_PTR)(void); // or whatever

FUNC_PTR some_function()
{
...
}

It is however impossible to define a function that returns a pointer to
itself, that would mean an infinite recursion in the type of that function.

john
Jul 22 '05 #2
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6ahbn$9ppkf$1@ID-> "Boris Sargos" <bs*****@wanadoo.fr> wrote in
message
I need to write a function that returns a pointer on a function. Is it
possible, and which is the syntax ?
Trying to do that without using a typedef is a real test of your
understanding of type declarations. The easy way is with a typedef
Without typedef I think this is it:

void (*some_function())();

This is a function some_function taking no arguments and returning a pointer
to a function taking no arguments and returning a void. It's similar to a
function taking a reference to an array of elements. But of course, what
you have below is cleaner:
typedef void (*FUNC_PTR)(void); // or whatever

FUNC_PTR some_function()
{
...
}

It is however impossible to define a function that returns a pointer to
itself, that would mean an infinite recursion in the type of that

function.

This makes sense, because of

typedef FUNC_PTR (*FUNC_PTR)(void); // or whatever

But what about

void (*)(*some_function())();

Just guessing. Could be wrong.
Jul 22 '05 #3

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:c7********************@bgtnsc04-news.ops.worldnet.att.net...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6ahbn$9ppkf$1@ID-> "Boris Sargos" <bs*****@wanadoo.fr> wrote in
message
I need to write a function that returns a pointer on a function. Is it
possible, and which is the syntax ?
Trying to do that without using a typedef is a real test of your
understanding of type declarations. The easy way is with a typedef


Without typedef I think this is it:

void (*some_function())();


That's right.
This is a function some_function taking no arguments and returning a pointer to a function taking no arguments and returning a void. It's similar to a
function taking a reference to an array of elements. But of course, what
you have below is cleaner:
typedef void (*FUNC_PTR)(void); // or whatever

FUNC_PTR some_function()
{
...
}

It is however impossible to define a function that returns a pointer to
itself, that would mean an infinite recursion in the type of that function.

This makes sense, because of

typedef FUNC_PTR (*FUNC_PTR)(void); // or whatever

But what about

void (*)(*some_function())();

Just guessing. Could be wrong.


Syntax error on my compiler. If it's not possible with a typedef I don't see
why you think it might be possible without a typedef.

john

Jul 22 '05 #4

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

Similar topics

5
by: amit kumar | last post by:
I am calling a function which returns pointer to a map. The declaration of the map is map<int,vectxyz*>. vectxyz is a vector containing pointer to a class xyz. For map<int,vectxyz*>* p1 In the...
10
by: Dirk Vanhaute | last post by:
I have only small knowledge of c++, but I would like to compile the example in http://support.microsoft.com/kb/q246772/ HOWTO: Retrieve and Set the Default Printer in Windows I included "#include...
8
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
4
by: ranjmis | last post by:
Hi all, I have come across a piece of code wherein a function returns a function pointer as it seems to me but not very clear from the prototype. As shown below - although return type is void...
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: ...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
4
by: Jeffrey Spoon | last post by:
Hello, I am trying to make a simple function that returns a pointer to another function. In my header file I've declared my function pointer: void (*pStateFunction) (void); //assume the function...
11
by: Antoninus Twink | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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,...

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.