Skip to contentSkip to conversation

anything to do with pointer to functions maybe?

DISCUSSION soraberri 3 participants 4 messages Dec 23, 2004 – Dec 30, 2004

lore
Share & tools
lore

soraberri

author
#0 lore raw

Hi all!
I'm having troubles understanding code:

what type is __sighandler_t if it is defined this way:? typedef void (*__sighandler_t) (int);

and then, what does this definition mean:
#define SIG_IGN ((__sighandler_t) 1)

thanks very much if you know and you tell me ;·)

regards

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#1 lore raw

On Thu, 2004-12-23 11:24:39 +0100, soraberri <[email protected]> wrote in message <[email protected]>:

Quoted 5 lines

Hi all!
I'm having troubles understanding code:

what type is __sighandler_t if it is defined this way:? typedef void (*__sighandler_t) (int);

We define that any signal handling function doesn't return anything useful (void, that is), and all these functions get a single argument (of type int) supplied.

Quoted 2 lines

and then, what does this definition mean:
#define SIG_IGN ((__sighandler_t) 1)

Also, we don't want *all* signals to actually be handled. Some are to be ignored. Since we can only use function pointers (now of type __sighandler_t), we need to have a special magic value (1 in this case) that we can use.

However, "1" alone doesn't make a good function pointer, nor is it a function at all or does accept arguments. To "look" like a function pointer of correct type, "1" is casted to be of __sighandler_t.

There would have been another approach: we could have written a small function that does nothing and could be used instead. However, it's address could change, so it doesn't work well as an API. This is why instead a distenct value (1) is used instead.

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
author
#2 lore raw
Quoted 28 lines · Jan-Benedict Glaw

On Thu, 2004-12-23 11:24:39 +0100, soraberri <[email protected]> wrote in message <[email protected]>:

Quoted 5 lines

Hi all!
I'm having troubles understanding code:

what type is __sighandler_t if it is defined this way:? typedef void (*__sighandler_t) (int);

We define that any signal handling function doesn't return anything useful (void, that is), and all these functions get a single argument (of type int) supplied.

Quoted 2 lines

and then, what does this definition mean:
#define SIG_IGN ((__sighandler_t) 1)

Also, we don't want *all* signals to actually be handled. Some are to be ignored. Since we can only use function pointers (now of type __sighandler_t), we need to have a special magic value (1 in this case) that we can use.

However, "1" alone doesn't make a good function pointer, nor is it a function at all or does accept arguments. To "look" like a function pointer of correct type, "1" is casted to be of __sighandler_t.

Quoted 7 lines

There would have been another approach: we could have written a small function that does nothing and could be used instead. However, it's address could change, so it doesn't work well as an API. This is why instead a distenct value (1) is used instead.

MfG, JBG

Thanks Jan, I think I have understood...

regards and have happy days

#3 lore raw
In reply to soraberri
Quoted 4 lines · soraberri

I'm having troubles understanding code:

what type is __sighandler_t if it is defined this way:? typedef void (*__sighandler_t) (int);

Pointer to function taking an int argument and returning void.

C's type syntax uses the same grammatical structure as expressions, so the above is interpreted as saying that the expression:

	(*__sighandler_t) (int)

has type "void".

The syntax <expr>(<args>) indicates a function call where the function is <expr> and the arguments are <args>, so:

	(*__sighandler_t) (int)

is a function call where (*__sighandler_t) is the function and the arguments consist of a single integer. Coupled with the knowledge that the overall type is "void", the expression:

	(*__sighandler_t)

must be a function taking an integer argument and returning void.

If an expression *P has type T then the type of P is "pointer to T", so __sighandler_t must be a pointer to a function taking an integer argument and returning void.

Signature · 1 line
Glynn Clements <[email protected]>