Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

(int(*)(int)) ?

34 views
Skip to first unread message

Christopher Pisz

unread,
Mar 31, 2016, 3:35:13 PM3/31/16
to
Can someone break this cast down for me?
Example: (int (*)(int))std::toupper

What are each of the parts in the brackets?

I am see C-style cast (int)myvariable, but never
(type(otherstuff)(morestuff))function



--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

Scott Lurndal

unread,
Mar 31, 2016, 3:40:42 PM3/31/16
to
Christopher Pisz <nos...@notanaddress.com> writes:
>Can someone break this cast down for me?
>Example: (int (*)(int))std::toupper
>
>What are each of the parts in the brackets?
>
>I am see C-style cast (int)myvariable, but never
>(type(otherstuff)(morestuff))function

It's a function pointer to a function returning int with a single int parameter.

Jens Thoms Toerring

unread,
Mar 31, 2016, 4:52:31 PM3/31/16
to
Christopher Pisz <nos...@notanaddress.com> wrote:
> Can someone break this cast down for me?
> Example: (int (*)(int))std::toupper

If you have a defintion like this

int (*foo)(double);

'foo' is a pointer variable to a function that takes an double
as its (only) argument and returns an int.

And, in that spirit,

(int (*)(int)) std::toupper

casts the address of the std::toupper function to a pointer
to a function that takes an int argument and returns an int.

You can also use this with typedefs, e.g.

typedef void (*sighandler_t)(int);

makes 'sighandler_t a type that is pointer to a function that
takes an int argument and returns void. This is used in the
declaration of the UNIX signal() function which sets up a
handler function (taking an int and returning void) for the
signal numbered 'signo' (and returns the previous handler):

sighandler_t signal(int signum, sighandler_t handler);

which would be a handful to parse without the typedef:

void (signal(int, void (*)(int)))(int, void (*)(int));

(I hope I got that right;-)

> What are each of the parts in the brackets?

> I am see C-style cast (int)myvariable, but never
> (type(otherstuff)(morestuff))function

That's probably not what your'e going to see (at least I don't
know what it would be supposed to mean;-), it's more more like

(rettype (*)(argtype1, argtype2)) function

which casts the address of 'function' to a pointer to a function
which returns 'rettype' and accepts two arguments, the first one
of type 'argtype1' and the second of type 'argtype2'.

Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Kalle Olavi Niemitalo

unread,
Apr 3, 2016, 10:27:26 AM4/3/16
to
j...@toerring.de (Jens Thoms Toerring) writes:

> void (signal(int, void (*)(int)))(int, void (*)(int));
>
> (I hope I got that right;-)

Nope, it's void (*signal(int signum, void (*handler)(int)))(int);
0 new messages