473,949 Members | 22,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2213
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 oldUSR1sighandl er = signal(SIGUSR1, hello1);
sighandler_t oldUSR2sighandl er = signal(SIGUSR2, hello2);
while (EOF != getchar());
// restore old signal handlers
signal(SIGUSR2, oldUSR2sighandl er);
signal(SIGUSR1, oldUSR1sighandl er);
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**********@n oos.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**********@n oos.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
2817
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 _build_used(): .... y = x + 1 .... return x, y ....
4
3025
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 comments in all three files, then it is ok in both compilers.
8
1164
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; /* statement 2 */ func_a(); /* statement 3 */ ...
7
2313
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
7642
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 be successful but not linking. Can somebody help me in telling the exact reason why compiler cannot find the reference of template class member function definition defined in cpp file?? following problem is not linking (undefined reference of...
6
3645
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 it gave a syntax error ("Invalid syntax") with caret pointing to the 'd' of the def keyword.
7
1865
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
1589
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
2300
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 meaning of "..."? I didn't find it in K&R book, and looking for "..." on the web doesn't seem to be productive. The actual
0
10170
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9990
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11188
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10699
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8264
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7435
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6128
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.