446,191 Members | 756 Online
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

# typedef void foo_t()

 P: n/a How does typedef work for foo2_t? What can one do with foo2_t? ------ bar.c ------ void foo() {} typedef void (*foo1_t)(); typedef void foo2_t(); /* How does typedef work here? */ int main() { foo1_t foo1 = foo; foo2_t foo2 = foo; /* Line 9 */ return 0; } ------------------- GNU gcc 4.0.1 bar.c: In function `main': bar.c:9: error: function `foo2' is initialized like a variable -- Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn Dec 9 '05 #1
4 Replies

 P: n/a In article , Alex Vinokur wrote: typedef void foo2_t(); /* How does typedef work here? */ ....foo2_t foo2 = foo; /* Line 9 */ This would be legal: foo2_t *foo2 = foo; -- Richard Dec 9 '05 #2

 P: n/a Alex Vinokur said: How does typedef work for foo2_t? What can one do with foo2_t? ------ bar.c ------ void foo() {} typedef void (*foo1_t)(); This means that foo1_t x; is a declaration that x is a pointer to a function taking unknown arguments and returning void. typedef void foo2_t(); /* How does typedef work here? */ This means that foo2_t y; is a declaration that y is a function taking unknown arguments and returning void. Because it's a function, it can't be instantiated in the same way an object can. Nevertheless, this is a useful technique. If the topic cops can just hold their fire for a second... typedef LRESULT RJH_MESSAGE_HANDLER(HWND, UINT, WPARAM, LPARAM); RJH_MESSAGE_HANDLER DebuggerCreate; RJH_MESSAGE_HANDLER DebuggerDestroy; RJH_MESSAGE_HANDLER DebuggerReset; RJH_MESSAGE_HANDLER DebuggerDisplay; is valid C, and (arguably) is neater than: LRESULT DebuggerCreate(HWND, UINT, WPARAM, LPARAM); LRESULT DebuggerDestroy(HWND, UINT, WPARAM, LPARAM); LRESULT DebuggerReset(HWND, UINT, WPARAM, LPARAM); LRESULT DebuggerDisplay(HWND, UINT, WPARAM, LPARAM); -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at above domain (but drop the www, obviously) Dec 9 '05 #3

 P: n/a Richard Heathfield writes: Alex Vinokur said: How does typedef work for foo2_t? What can one do with foo2_t? ------ bar.c ------ void foo() {} typedef void (*foo1_t)(); This means that foo1_t x; is a declaration that x is a pointer to a function taking unknown arguments and returning void. typedef void foo2_t(); /* How does typedef work here? */ This means that foo2_t y; is a declaration that y is a function taking unknown arguments and returning void. Because it's a function, it can't be instantiated in the same way an object can. Nevertheless, this is a useful technique. If the topic cops can just hold their fire for a second... I don't see anything in what you write that could be considered off topic, so no need to ask the topic cops to shut up... (And I haven't written one single line of code for windows apps in more than five years). typedef LRESULT RJH_MESSAGE_HANDLER(HWND, UINT, WPARAM, LPARAM); RJH_MESSAGE_HANDLER DebuggerCreate; RJH_MESSAGE_HANDLER DebuggerDestroy; RJH_MESSAGE_HANDLER DebuggerReset; RJH_MESSAGE_HANDLER DebuggerDisplay; Interesting. I've never used function declaration like these, and I agree that they could be prefered in some situations... /Niklas Norrthon Dec 12 '05 #4

 P: n/a Niklas Norrthon said: Richard Heathfield writes: typedef LRESULT RJH_MESSAGE_HANDLER(HWND, UINT, WPARAM, LPARAM); RJH_MESSAGE_HANDLER DebuggerCreate; RJH_MESSAGE_HANDLER DebuggerDestroy; RJH_MESSAGE_HANDLER DebuggerReset; RJH_MESSAGE_HANDLER DebuggerDisplay; Interesting. I've never used function declaration like these, and I agree that they could be prefered in some situations... I forgot to say earlier that I actually prefer to typedef function types rather than function pointer types, because: void foo(RJH_MESSAGE_HANDLER *fptr) { ... whatever ... } reminds me that I'm dealing with a pointer type. I don't like hiding pointers with typedef. Just a style thing really. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at above domain (but drop the www, obviously) Dec 12 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.