Defining a function which returns a function pointer which also returns a function pointer without...
How does Vivi differ from other Black Mages?
Do Milankovitch Cycles fully explain climate change?
Is Salesforce Classic being deprecated?
How can I fix a framing mistake so I can drywall?
Georgian capital letter “Ⴒ” (“tar”) in pdfLaTeX
What's the biggest organic molecule that could have a smell?
Why did it become so much more expensive to start a university?
The case of the pranking snowplow
Why should I always enable compiler warnings?
Does a gnoll speak both Gnoll and Abyssal, or is Gnoll a dialect of Abyssal?
Are programming languages necessary/useful for operations research practitioner?
What is Japanese Language Stack Exchange called in Japanese?
I asked for a graduate student position from a professor. He replied "welcome". What does that mean?
Do ibuprofen or paracetamol cause hearing loss?
Job offer without any details but asking me to withdraw other applications - is it normal?
Are Democrats more likely to believe Astrology is a science?
Can I disable a battery powered device by reversing half of its batteries?
Random point on a sphere
Where does the expression "triple-A" comes from?
Can I toggle Do Not Disturb on/off on my Mac as easily as I can on my iPhone?
My employer wants me to do a work of 6 months in just 2 months
Can the UK veto its own extension request?
Which ping implementation is Cygwin using?
Writing a worded mathematical expression
Defining a function which returns a function pointer which also returns a function pointer without typedefs
How do function pointers in C work?Understanding typedefs for function pointers in CTypedef function pointer?Returning function pointer typetypedef function pointers and extern keywordError with function pointer returning a (void *)Passing capturing lambda as function pointerPass typedefed function pointerReturn a function pointer without a typedef
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I am trying to really understand function pointers without using typedef
but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function.
#include <stdio.h>
void odd() { printf("odd!n"); }
void even() { printf("even!n"); }
void (*get_pfn(int i))()
{
return i % 2 == 0 ? &even : &odd;
}
__SIGNATURE__
{
return &get_pfn;
}
int main()
{
get_pfn_pfn()(1)();
get_pfn_pfn()(2)();
return 0;
}
To make this work, what does __SIGNATURE__
have to be?
c function function-pointers
add a comment |
I am trying to really understand function pointers without using typedef
but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function.
#include <stdio.h>
void odd() { printf("odd!n"); }
void even() { printf("even!n"); }
void (*get_pfn(int i))()
{
return i % 2 == 0 ? &even : &odd;
}
__SIGNATURE__
{
return &get_pfn;
}
int main()
{
get_pfn_pfn()(1)();
get_pfn_pfn()(2)();
return 0;
}
To make this work, what does __SIGNATURE__
have to be?
c function function-pointers
6
Good question - now don't ever do this again. :)
– Michael Dorgan
11 hours ago
5
use cdecl
– Antti Haapala
10 hours ago
add a comment |
I am trying to really understand function pointers without using typedef
but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function.
#include <stdio.h>
void odd() { printf("odd!n"); }
void even() { printf("even!n"); }
void (*get_pfn(int i))()
{
return i % 2 == 0 ? &even : &odd;
}
__SIGNATURE__
{
return &get_pfn;
}
int main()
{
get_pfn_pfn()(1)();
get_pfn_pfn()(2)();
return 0;
}
To make this work, what does __SIGNATURE__
have to be?
c function function-pointers
I am trying to really understand function pointers without using typedef
but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function.
#include <stdio.h>
void odd() { printf("odd!n"); }
void even() { printf("even!n"); }
void (*get_pfn(int i))()
{
return i % 2 == 0 ? &even : &odd;
}
__SIGNATURE__
{
return &get_pfn;
}
int main()
{
get_pfn_pfn()(1)();
get_pfn_pfn()(2)();
return 0;
}
To make this work, what does __SIGNATURE__
have to be?
c function function-pointers
c function function-pointers
edited 2 hours ago
Justin
15.3k10 gold badges63 silver badges106 bronze badges
15.3k10 gold badges63 silver badges106 bronze badges
asked 11 hours ago
JasJas
5095 silver badges14 bronze badges
5095 silver badges14 bronze badges
6
Good question - now don't ever do this again. :)
– Michael Dorgan
11 hours ago
5
use cdecl
– Antti Haapala
10 hours ago
add a comment |
6
Good question - now don't ever do this again. :)
– Michael Dorgan
11 hours ago
5
use cdecl
– Antti Haapala
10 hours ago
6
6
Good question - now don't ever do this again. :)
– Michael Dorgan
11 hours ago
Good question - now don't ever do this again. :)
– Michael Dorgan
11 hours ago
5
5
use cdecl
– Antti Haapala
10 hours ago
use cdecl
– Antti Haapala
10 hours ago
add a comment |
4 Answers
4
active
oldest
votes
It has to return a function pointer to a function that takes an int
and returns a function pointer:
void (*(*get_pfn_pfn(void))(int))(void) {
return &get_pfn;
}
more lines:
void (*
(*
get_pfn_pfn(void) // this is our function
)(int i) // this is get_pfn(int)
)(void) // this is odd() or even()
{
return &get_pfn;
}
The void
s can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void
inside function parameter list. The same way it's best to change get_pfn
to void (*get_pfn(int i))(void)
. For example try calling from get_pfn(1)("some arg", "some other arg");
. A C compiler will not give a warning, as empty ()
denote unknown arguments. To say that function takes no arguments, you have to (void)
.
For many the sequences of braces, especially ))(
, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:
typedef void get_pfn_func_t(void);
get_pfn_func_t *get_pfn(int i) {
return i % 2 == 0 ? &even : &odd;
}
typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
get_pfn_pfn_func_t *get_pfn_pfn(void) {
return &get_pfn;
}
2
This one confirmed to work with online C check.
– Michael Dorgan
11 hours ago
Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!
– Jas
11 hours ago
1
@Jas You may like it but I don't think it is recommended to write code in this manner.
– machine_1
11 hours ago
I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com
– Alexander
2 hours ago
add a comment |
The return type of the function get_pfn
is -
void (*) ();
So type of &get_pfn
is -
void (*(*)(int))()
Now, this function returns this type, hence it's signature will be -
void (*(*(foo)())(int))()
You can verify this by typing this in cdecl.org
Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(
– Jas
11 hours ago
1
@Jas, sorry I messed up the order of the(int)
. I thought your odd even functions take a param int. I will correct it.
– Ajay Brahmakshatriya
11 hours ago
1
@Jas, I have corrected my answer,
– Ajay Brahmakshatriya
11 hours ago
add a comment |
Function pointers without a typedef
can be tricky to work with. To figure them out, you work from the inside out.
So let's break down exactly how we come up with the correct function signature.
get_pfn_pfn
is a function:
get_pfn_pfn()
Which takes no parameters:
get_pfn_pfn(void)
And returns a pointer:
*get_pfn_pfn(void)
To a function:
(*get_pfn_pfn(void))()
Which takes an int
parameter:
(*get_pfn_pfn(void))(int)
And returns a pointer:
*(*get_pfn_pfn(void))(int)
To a function:
(*(*get_pfn_pfn(void))(int))()
Which takes no parameters:
(*(*get_pfn_pfn(void))(int))(void)
And returns nothing (i.e. void
):
void (*(*get_pfn_pfn(void))(int))(void)
Of course, using typedef
's simplifies this greatly.
First the type for even
and odd
:
typedef void (*func1)(void);
Which we can then apply to get_pfn
:
func1 get_pfn(int) { ... }
Then the type for this function:
typedef func1 (*func2)(int);
Which we can apply to get_pfn_pfn
:
func2 get_pfn_pfn(void) { ... }
add a comment |
it's this way:
void (*(*get_pfn_pfn(void))(int))()
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57875417%2fdefining-a-function-which-returns-a-function-pointer-which-also-returns-a-functi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
It has to return a function pointer to a function that takes an int
and returns a function pointer:
void (*(*get_pfn_pfn(void))(int))(void) {
return &get_pfn;
}
more lines:
void (*
(*
get_pfn_pfn(void) // this is our function
)(int i) // this is get_pfn(int)
)(void) // this is odd() or even()
{
return &get_pfn;
}
The void
s can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void
inside function parameter list. The same way it's best to change get_pfn
to void (*get_pfn(int i))(void)
. For example try calling from get_pfn(1)("some arg", "some other arg");
. A C compiler will not give a warning, as empty ()
denote unknown arguments. To say that function takes no arguments, you have to (void)
.
For many the sequences of braces, especially ))(
, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:
typedef void get_pfn_func_t(void);
get_pfn_func_t *get_pfn(int i) {
return i % 2 == 0 ? &even : &odd;
}
typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
get_pfn_pfn_func_t *get_pfn_pfn(void) {
return &get_pfn;
}
2
This one confirmed to work with online C check.
– Michael Dorgan
11 hours ago
Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!
– Jas
11 hours ago
1
@Jas You may like it but I don't think it is recommended to write code in this manner.
– machine_1
11 hours ago
I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com
– Alexander
2 hours ago
add a comment |
It has to return a function pointer to a function that takes an int
and returns a function pointer:
void (*(*get_pfn_pfn(void))(int))(void) {
return &get_pfn;
}
more lines:
void (*
(*
get_pfn_pfn(void) // this is our function
)(int i) // this is get_pfn(int)
)(void) // this is odd() or even()
{
return &get_pfn;
}
The void
s can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void
inside function parameter list. The same way it's best to change get_pfn
to void (*get_pfn(int i))(void)
. For example try calling from get_pfn(1)("some arg", "some other arg");
. A C compiler will not give a warning, as empty ()
denote unknown arguments. To say that function takes no arguments, you have to (void)
.
For many the sequences of braces, especially ))(
, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:
typedef void get_pfn_func_t(void);
get_pfn_func_t *get_pfn(int i) {
return i % 2 == 0 ? &even : &odd;
}
typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
get_pfn_pfn_func_t *get_pfn_pfn(void) {
return &get_pfn;
}
2
This one confirmed to work with online C check.
– Michael Dorgan
11 hours ago
Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!
– Jas
11 hours ago
1
@Jas You may like it but I don't think it is recommended to write code in this manner.
– machine_1
11 hours ago
I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com
– Alexander
2 hours ago
add a comment |
It has to return a function pointer to a function that takes an int
and returns a function pointer:
void (*(*get_pfn_pfn(void))(int))(void) {
return &get_pfn;
}
more lines:
void (*
(*
get_pfn_pfn(void) // this is our function
)(int i) // this is get_pfn(int)
)(void) // this is odd() or even()
{
return &get_pfn;
}
The void
s can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void
inside function parameter list. The same way it's best to change get_pfn
to void (*get_pfn(int i))(void)
. For example try calling from get_pfn(1)("some arg", "some other arg");
. A C compiler will not give a warning, as empty ()
denote unknown arguments. To say that function takes no arguments, you have to (void)
.
For many the sequences of braces, especially ))(
, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:
typedef void get_pfn_func_t(void);
get_pfn_func_t *get_pfn(int i) {
return i % 2 == 0 ? &even : &odd;
}
typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
get_pfn_pfn_func_t *get_pfn_pfn(void) {
return &get_pfn;
}
It has to return a function pointer to a function that takes an int
and returns a function pointer:
void (*(*get_pfn_pfn(void))(int))(void) {
return &get_pfn;
}
more lines:
void (*
(*
get_pfn_pfn(void) // this is our function
)(int i) // this is get_pfn(int)
)(void) // this is odd() or even()
{
return &get_pfn;
}
The void
s can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void
inside function parameter list. The same way it's best to change get_pfn
to void (*get_pfn(int i))(void)
. For example try calling from get_pfn(1)("some arg", "some other arg");
. A C compiler will not give a warning, as empty ()
denote unknown arguments. To say that function takes no arguments, you have to (void)
.
For many the sequences of braces, especially ))(
, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:
typedef void get_pfn_func_t(void);
get_pfn_func_t *get_pfn(int i) {
return i % 2 == 0 ? &even : &odd;
}
typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
get_pfn_pfn_func_t *get_pfn_pfn(void) {
return &get_pfn;
}
edited 11 hours ago
answered 11 hours ago
Kamil CukKamil Cuk
21.5k2 gold badges9 silver badges37 bronze badges
21.5k2 gold badges9 silver badges37 bronze badges
2
This one confirmed to work with online C check.
– Michael Dorgan
11 hours ago
Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!
– Jas
11 hours ago
1
@Jas You may like it but I don't think it is recommended to write code in this manner.
– machine_1
11 hours ago
I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com
– Alexander
2 hours ago
add a comment |
2
This one confirmed to work with online C check.
– Michael Dorgan
11 hours ago
Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!
– Jas
11 hours ago
1
@Jas You may like it but I don't think it is recommended to write code in this manner.
– machine_1
11 hours ago
I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com
– Alexander
2 hours ago
2
2
This one confirmed to work with online C check.
– Michael Dorgan
11 hours ago
This one confirmed to work with online C check.
– Michael Dorgan
11 hours ago
Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!
– Jas
11 hours ago
Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!
– Jas
11 hours ago
1
1
@Jas You may like it but I don't think it is recommended to write code in this manner.
– machine_1
11 hours ago
@Jas You may like it but I don't think it is recommended to write code in this manner.
– machine_1
11 hours ago
I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com
– Alexander
2 hours ago
I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com
– Alexander
2 hours ago
add a comment |
The return type of the function get_pfn
is -
void (*) ();
So type of &get_pfn
is -
void (*(*)(int))()
Now, this function returns this type, hence it's signature will be -
void (*(*(foo)())(int))()
You can verify this by typing this in cdecl.org
Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(
– Jas
11 hours ago
1
@Jas, sorry I messed up the order of the(int)
. I thought your odd even functions take a param int. I will correct it.
– Ajay Brahmakshatriya
11 hours ago
1
@Jas, I have corrected my answer,
– Ajay Brahmakshatriya
11 hours ago
add a comment |
The return type of the function get_pfn
is -
void (*) ();
So type of &get_pfn
is -
void (*(*)(int))()
Now, this function returns this type, hence it's signature will be -
void (*(*(foo)())(int))()
You can verify this by typing this in cdecl.org
Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(
– Jas
11 hours ago
1
@Jas, sorry I messed up the order of the(int)
. I thought your odd even functions take a param int. I will correct it.
– Ajay Brahmakshatriya
11 hours ago
1
@Jas, I have corrected my answer,
– Ajay Brahmakshatriya
11 hours ago
add a comment |
The return type of the function get_pfn
is -
void (*) ();
So type of &get_pfn
is -
void (*(*)(int))()
Now, this function returns this type, hence it's signature will be -
void (*(*(foo)())(int))()
You can verify this by typing this in cdecl.org
The return type of the function get_pfn
is -
void (*) ();
So type of &get_pfn
is -
void (*(*)(int))()
Now, this function returns this type, hence it's signature will be -
void (*(*(foo)())(int))()
You can verify this by typing this in cdecl.org
edited 11 hours ago
answered 11 hours ago
Ajay BrahmakshatriyaAjay Brahmakshatriya
7,2453 gold badges16 silver badges36 bronze badges
7,2453 gold badges16 silver badges36 bronze badges
Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(
– Jas
11 hours ago
1
@Jas, sorry I messed up the order of the(int)
. I thought your odd even functions take a param int. I will correct it.
– Ajay Brahmakshatriya
11 hours ago
1
@Jas, I have corrected my answer,
– Ajay Brahmakshatriya
11 hours ago
add a comment |
Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(
– Jas
11 hours ago
1
@Jas, sorry I messed up the order of the(int)
. I thought your odd even functions take a param int. I will correct it.
– Ajay Brahmakshatriya
11 hours ago
1
@Jas, I have corrected my answer,
– Ajay Brahmakshatriya
11 hours ago
Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(
– Jas
11 hours ago
Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(
– Jas
11 hours ago
1
1
@Jas, sorry I messed up the order of the
(int)
. I thought your odd even functions take a param int. I will correct it.– Ajay Brahmakshatriya
11 hours ago
@Jas, sorry I messed up the order of the
(int)
. I thought your odd even functions take a param int. I will correct it.– Ajay Brahmakshatriya
11 hours ago
1
1
@Jas, I have corrected my answer,
– Ajay Brahmakshatriya
11 hours ago
@Jas, I have corrected my answer,
– Ajay Brahmakshatriya
11 hours ago
add a comment |
Function pointers without a typedef
can be tricky to work with. To figure them out, you work from the inside out.
So let's break down exactly how we come up with the correct function signature.
get_pfn_pfn
is a function:
get_pfn_pfn()
Which takes no parameters:
get_pfn_pfn(void)
And returns a pointer:
*get_pfn_pfn(void)
To a function:
(*get_pfn_pfn(void))()
Which takes an int
parameter:
(*get_pfn_pfn(void))(int)
And returns a pointer:
*(*get_pfn_pfn(void))(int)
To a function:
(*(*get_pfn_pfn(void))(int))()
Which takes no parameters:
(*(*get_pfn_pfn(void))(int))(void)
And returns nothing (i.e. void
):
void (*(*get_pfn_pfn(void))(int))(void)
Of course, using typedef
's simplifies this greatly.
First the type for even
and odd
:
typedef void (*func1)(void);
Which we can then apply to get_pfn
:
func1 get_pfn(int) { ... }
Then the type for this function:
typedef func1 (*func2)(int);
Which we can apply to get_pfn_pfn
:
func2 get_pfn_pfn(void) { ... }
add a comment |
Function pointers without a typedef
can be tricky to work with. To figure them out, you work from the inside out.
So let's break down exactly how we come up with the correct function signature.
get_pfn_pfn
is a function:
get_pfn_pfn()
Which takes no parameters:
get_pfn_pfn(void)
And returns a pointer:
*get_pfn_pfn(void)
To a function:
(*get_pfn_pfn(void))()
Which takes an int
parameter:
(*get_pfn_pfn(void))(int)
And returns a pointer:
*(*get_pfn_pfn(void))(int)
To a function:
(*(*get_pfn_pfn(void))(int))()
Which takes no parameters:
(*(*get_pfn_pfn(void))(int))(void)
And returns nothing (i.e. void
):
void (*(*get_pfn_pfn(void))(int))(void)
Of course, using typedef
's simplifies this greatly.
First the type for even
and odd
:
typedef void (*func1)(void);
Which we can then apply to get_pfn
:
func1 get_pfn(int) { ... }
Then the type for this function:
typedef func1 (*func2)(int);
Which we can apply to get_pfn_pfn
:
func2 get_pfn_pfn(void) { ... }
add a comment |
Function pointers without a typedef
can be tricky to work with. To figure them out, you work from the inside out.
So let's break down exactly how we come up with the correct function signature.
get_pfn_pfn
is a function:
get_pfn_pfn()
Which takes no parameters:
get_pfn_pfn(void)
And returns a pointer:
*get_pfn_pfn(void)
To a function:
(*get_pfn_pfn(void))()
Which takes an int
parameter:
(*get_pfn_pfn(void))(int)
And returns a pointer:
*(*get_pfn_pfn(void))(int)
To a function:
(*(*get_pfn_pfn(void))(int))()
Which takes no parameters:
(*(*get_pfn_pfn(void))(int))(void)
And returns nothing (i.e. void
):
void (*(*get_pfn_pfn(void))(int))(void)
Of course, using typedef
's simplifies this greatly.
First the type for even
and odd
:
typedef void (*func1)(void);
Which we can then apply to get_pfn
:
func1 get_pfn(int) { ... }
Then the type for this function:
typedef func1 (*func2)(int);
Which we can apply to get_pfn_pfn
:
func2 get_pfn_pfn(void) { ... }
Function pointers without a typedef
can be tricky to work with. To figure them out, you work from the inside out.
So let's break down exactly how we come up with the correct function signature.
get_pfn_pfn
is a function:
get_pfn_pfn()
Which takes no parameters:
get_pfn_pfn(void)
And returns a pointer:
*get_pfn_pfn(void)
To a function:
(*get_pfn_pfn(void))()
Which takes an int
parameter:
(*get_pfn_pfn(void))(int)
And returns a pointer:
*(*get_pfn_pfn(void))(int)
To a function:
(*(*get_pfn_pfn(void))(int))()
Which takes no parameters:
(*(*get_pfn_pfn(void))(int))(void)
And returns nothing (i.e. void
):
void (*(*get_pfn_pfn(void))(int))(void)
Of course, using typedef
's simplifies this greatly.
First the type for even
and odd
:
typedef void (*func1)(void);
Which we can then apply to get_pfn
:
func1 get_pfn(int) { ... }
Then the type for this function:
typedef func1 (*func2)(int);
Which we can apply to get_pfn_pfn
:
func2 get_pfn_pfn(void) { ... }
edited 10 hours ago
answered 11 hours ago
dbushdbush
112k15 gold badges127 silver badges159 bronze badges
112k15 gold badges127 silver badges159 bronze badges
add a comment |
add a comment |
it's this way:
void (*(*get_pfn_pfn(void))(int))()
New contributor
add a comment |
it's this way:
void (*(*get_pfn_pfn(void))(int))()
New contributor
add a comment |
it's this way:
void (*(*get_pfn_pfn(void))(int))()
New contributor
it's this way:
void (*(*get_pfn_pfn(void))(int))()
New contributor
edited 11 hours ago
New contributor
answered 11 hours ago
Abdelfettah BesbesAbdelfettah Besbes
31110 bronze badges
31110 bronze badges
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57875417%2fdefining-a-function-which-returns-a-function-pointer-which-also-returns-a-functi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
Good question - now don't ever do this again. :)
– Michael Dorgan
11 hours ago
5
use cdecl
– Antti Haapala
10 hours ago