473,408 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

would anyone help me this definition of func ptr?

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
Nov 14 '05 #1
6 2188
ooze wrote:
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); 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:
ps -a 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 ./main
and back at the second terminal:
ps -a 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
Nov 14 '05 #2
On 13 Jul 2004 18:21:58 -0700, fj***@163.net (ooze) wrote:
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>>
Nov 14 '05 #3
In 'comp.lang.c', fj***@163.net (ooze) wrote:
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);

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #4
Emmanuel Delahaye <em**********@noos.fr> wrote in message news:<Xn***************************@212.27.42.71>. ..
In 'comp.lang.c', fj***@163.net (ooze) wrote:
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);


what's the differnce of the two declarations?
Nov 14 '05 #5
Emmanuel Delahaye <em**********@noos.fr> wrote in message news:<Xn***************************@212.27.42.71>. ..
In 'comp.lang.c', fj***@163.net (ooze) wrote:
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);


what's the differnce of the two declarations?
Nov 14 '05 #6
In 'comp.lang.c', fj***@163.net (ooze) wrote:

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

OTOH
typedef void F_TYPE (int);

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


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).

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
4
by: jack | last post by:
Hi there, I have a short question about template function definition. 1) The following files will not compile in M$ VC++ but will have no problem with g++. 2) However, if I uncomment all...
8
by: Hongzheng Wang | last post by:
Hi, all. According to ISO stand, in any function definition block, the viariables definitions statements must be the first? That is, int func() { int i; /* statement 1 */ double a; /*...
7
by: G Fernandes | last post by:
Is the following an old-style(K&R) function definition or a new style(C89)? int (length, factor, multiple) { /* ... code ... */ return length; }
8
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
6
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
7
by: gsi | last post by:
Hi, I looked a while at this but couldnt convice myself of the following reasons in the code. class A{ int a; public: A():a(0){} void func() {int A::*ptr = &A::a;}
5
by: er | last post by:
hi, is anyone aware of a library that generalizes transform, for_each? e.g. transform( vec1.begin(), vec1.end(), vec2.begin(), vec3.begin(),
19
by: eotcl | last post by:
Dear All! While looking for the implementation of the printf function, I came across the implementation that used the following construct int printf(const char *format, ...) What is the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.