typedef void foo_t()

A

Alex Vinokur

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
 
R

Richard Tobin

Alex Vinokur said:
typedef void foo2_t(); /* How does typedef work here? */ ....
foo2_t foo2 = foo; /* Line 9 */

This would be legal:

foo2_t *foo2 = foo;

-- Richard
 
R

Richard Heathfield

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);
 
N

Niklas Norrthon

Richard Heathfield said:
Alex Vinokur said:


This means that

foo1_t x;

is a declaration that x is a pointer to a function taking unknown arguments
and returning void.


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
 
R

Richard Heathfield

Niklas Norrthon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top