would anyone help me this definition of func ptr?

O

ooze

In the unix like system, there is a func ptr signal defined as below
at the last line ,but I don't know
how to understand it. becoz the more general to define a func ptr is
the following
void (*signal)(int)
not
#include <signal.h>
void ( * signal(int SIG, void(*FUNC)(int)) )(int);

would anyone help me?
thanks much
 
E

E. Robert Tisdale

ooze said:
In the unix like system, there is a func ptr signal
defined as below at the last line but I don't know how to understand it.
becoz the more general to define a func ptr is the following:

void (*signal)(int);

#include <signal.h>
void ( * signal(int SIG, void(*FUNC)(int)) )(int);
> cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void hello1(int signum) {
fprintf(stdout, "%d = signum\n", signum);
}

void hello2(int signum) {
fprintf(stdout, "%d = signum\n", signum);
}

int main(int argc, char* argv[]) {
typedef void (*sighandler_t)(int);
// install new signal handlers
sighandler_t oldUSR1sighandler = signal(SIGUSR1, hello1);
sighandler_t oldUSR2sighandler = signal(SIGUSR2, hello2);
while (EOF != getchar());
// restore old signal handlers
signal(SIGUSR2, oldUSR2sighandler);
signal(SIGUSR1, oldUSR1sighandler);
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main

Then at a second terminal:
PID TTY TIME CMD
21080 pts/21 00:00:01 ssh
21217 pts/22 00:00:01 ssh
22016 pts/23 00:00:00 main
22018 pts/24 00:00:00 ps
> kill -s USR1 22016

and then, at the first terminal, I get:

10 = signum

and back at the second terminal:
PID TTY TIME CMD
21080 pts/21 00:00:01 ssh
21217 pts/22 00:00:01 ssh
22027 pts/23 00:00:00 main
22028 pts/24 00:00:00 ps
> kill -s USR2 22027

and back, at the first terminal, I get:

12 = signum
 
B

Barry Schwarz

In the unix like system, there is a func ptr signal defined as below
at the last line ,but I don't know
how to understand it. becoz the more general to define a func ptr is
the following
void (*signal)(int)
not
#include <signal.h>
void ( * signal(int SIG, void(*FUNC)(int)) )(int);

would anyone help me?
thanks much

It helps to redistribute the white space

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

This is a function prototype and not the definition of a pointer.

signal is function (not a pointer) that

takes two parameters
an int (called SIG here but that is superfluous)
a pointer (called FUNC but also superfluous) to a function
that takes one parameter (int) and returns void

and returns a pointer to a function that takes an int and returns
void


<<Remove the del for email>>
 
E

Emmanuel Delahaye

In said:
In the unix like system, there is a func ptr signal defined as below
at the last line ,but I don't know
how to understand it. becoz the more general to define a func ptr is
the following
void (*signal)(int)
not
#include <signal.h>
void ( * signal(int SIG, void(*FUNC)(int)) )(int);

To make it simple, it's a function that returns a pointer to a function.
One of the parameter of the function is a pointer to a function. Probably the
most terrible syntax of the C language!

It is exactly the case where the use of a typedef is worthy.

typedef void F_TYPE (int);

F_TYPE *signal (int SIG, F_TYPE *FUNC);
 
O

ooze

Emmanuel Delahaye said:
To make it simple, it's a function that returns a pointer to a function.
One of the parameter of the function is a pointer to a function. Probably the
most terrible syntax of the C language!

It is exactly the case where the use of a typedef is worthy.

typedef void F_TYPE (int);

F_TYPE *signal (int SIG, F_TYPE *FUNC);

what's the differnce of the two declarations?
 
O

ooze

Emmanuel Delahaye said:
To make it simple, it's a function that returns a pointer to a function.
One of the parameter of the function is a pointer to a function. Probably the
most terrible syntax of the C language!

It is exactly the case where the use of a typedef is worthy.

typedef void F_TYPE (int);

F_TYPE *signal (int SIG, F_TYPE *FUNC);

what's the differnce of the two declarations?
 
E

Emmanuel Delahaye

In 'comp.lang.c', (e-mail address removed) (ooze) wrote:

OOH

OTOH
what's the differnce of the two declarations?

The point is that the semantic is the same, but the syntax is different. (It
tends to be clearer, well, I hope).
 

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,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top