Timeline for answer to Typedef function pointer? by Déjà vu
Current License: CC BY-SA 4.0
Post Revisions
21 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Dec 10, 2023 at 1:11 | comment | added | Alvin Cheng |
Some times when I'm being lazy, I just #define it all the time π
|
|
| Oct 10, 2021 at 8:22 | history | edited | Yun | CC BY-SA 4.0 |
Remove unnecessary leading whitespace from the first two code snippets.
|
| May 25, 2020 at 11:21 | comment | added | user5730329 |
awkwardly enough you can even do typedef returntype smth() and then declare a pointer to it like smth* f = &somemethod
|
|
| Aug 16, 2019 at 8:48 | comment | added | Lewis Chan |
Furthermore, google test writes typedef like this, very weird. @ring-Ø Could you please explain some ?
|
|
| May 17, 2019 at 7:00 | comment | added | cmaster - reinstate monica |
Note that a function pointer is not necessarily just an "address". What it actually is, depends on your platform. On some platforms, it's really just the address of the first byte of the functions' machine code, on other platforms it may be something different. On the PPC platform, for instance, a function pointer was actually a pair of addresses: One gave the start of the machine code, the other the associated globals table. So sizeof(void (*)()) == 2*sizeof(void*). Other platforms may use something even more strange - function pointers are just not the same as "normal" pointers!
|
|
| May 2, 2018 at 6:06 | comment | added | user2356685 | It would be nice to talk a little bit more about the semantics of a C-style function pointer and a C++ object-oriented function pointer, specifically about how much more memory it takes in C++ and typedef just doesn't work for OO function pointers. | |
| Mar 28, 2017 at 20:32 | comment | added | Jon Spencer | While a function usually just decays to a function pointer, this is not always the case. It is safer to just always use the & rather than relying on the decay always occurring. (Or you could just wait until the compiler complains and then add it - up to you :-) ) | |
| Jun 9, 2016 at 8:59 | history | edited | Déjà vu | CC BY-SA 3.0 |
English wording
|
| Mar 24, 2015 at 13:26 | history | edited | Déjà vu | CC BY-SA 3.0 |
vocabulary: changed 'keyword'. Also rephrased an inadequate sentence.
|
| Mar 24, 2015 at 11:16 | comment | added | Cheers and hth. - Alf |
Regarding "Is a function pointer created to store the memory address of a function? Yes, ", no, not in this code. Also, stating that the name introduced by a typedef is a "keyword" is incorrect. And regarding the evaluation "the syntax is appropriate", both the C and C++ language creators disagree, calling it a failed experiment.
|
|
| S Apr 27, 2014 at 21:09 | history | suggested | matthugs | CC BY-SA 3.0 |
fixed misspelling, changed function name to be more descriptive (the latter fix only to satisfy SO's six-character minimum)
|
| Apr 27, 2014 at 21:04 | review | Suggested edits | |||
| S Apr 27, 2014 at 21:09 | |||||
| Dec 4, 2013 at 23:47 | comment | added | Dan |
The reason the it looks like that is that typedef is a storage class specifier, like extern or static. You can think of it changing the storage class to type definitions. source. Thinking of it this way makes the syntax easier to remember.
|
|
| May 3, 2013 at 5:07 | comment | added | dchhetri |
I guess I'm just confused about the ordering. With typedef int (*func)(int), I understand that func is an alias, just a little confused because the alias is tangled with the type. Going by typedef int INT as an example I would be more of ease if typedef function pointer was of form typedef int(*function)(int) FUNC_1. That way I can see the type and alias in two separate token instead of being meshed into one.
|
|
| May 3, 2013 at 4:01 | comment | added | Jonathan Leffler |
@user814628: It is not clear quite what you're asking. With typedef int newname, you are making newname into an alias for int. With typedef int (*func)(int), you are making func into an alias for int (*)(int) β a pointer to function taking an int argument and returning an int value.
|
|
| May 3, 2013 at 3:58 | comment | added | Jonathan Leffler |
@pranavk: Yes, square and &square (and, indeed, *square and **square) all refer to the same function pointer.
|
|
| May 3, 2013 at 3:56 | comment | added | dchhetri |
Question, in your first typedef example you have of the form typedef type alias but with function pointers there only seems to be 2 arguments, typedef type. Is alias defaulted to the name specified in type argument?
|
|
| Mar 5, 2013 at 13:32 | comment | added | pranavk | in the last example, wouldn't just 'square' refer to the same thing i.e pointer to the function instead of using &square. | |
| Feb 6, 2013 at 11:20 | history | edited | Déjà vu | CC BY-SA 3.0 |
cleaning, added exemple
|
| Nov 28, 2010 at 5:26 | vote | accept | Jack Harvin | ||
| Nov 28, 2010 at 5:13 | history | answered | Déjà vu | CC BY-SA 2.5 |