From: Allan Adler on 26 Feb 2007 11:34 Can someone please tell me what is wrong with the following program and how to fix it? It is running on a PC running RedHat 7.1 Linux. #include <signal.h> int err; void (*onintr)(int); #define YES 1 int main() { if (signal(SIGINT,SIG_IGN) != SIG_IGN) {signal(SIGINT,onintr);} return 0; } void (*onintr)(int) { signal(SIGINT, onintr); if (err == YES) exit(1); /* exit(err == YES);*/ return; } The compiler (gcc 2.96) complains that there is a syntax error before the left brace of the body of onintr. The compiler also doesn't like the original exit(err == YES) and I wrote if (err == YES) exit(1); return; as a way to appease it. -- Ignorantly, Allan Adler <ara(a)zurich.csail.mit.edu> * Disclaimer: I am a guest and *not* a member of the MIT CSAIL. My actions and * comments do not reflect in any way on MIT. Also, I am nowhere near Boston.
From: user923005 on 26 Feb 2007 11:43 On Feb 26, 8:34 am, Allan Adler <a...(a)nestle.csail.mit.edu> wrote: > Can someone please tell me what is wrong with the following program and > how to fix it? It is running on a PC running RedHat 7.1 Linux. > > #include <signal.h> > > int err; > void (*onintr)(int); > #define YES 1 > > int main() > { > if (signal(SIGINT,SIG_IGN) != SIG_IGN) {signal(SIGINT,onintr);} > return 0; > > } > > void (*onintr)(int) > { > signal(SIGINT, onintr); > if (err == YES) exit(1); > /* exit(err == YES);*/ > return; > > } > > The compiler (gcc 2.96) complains that there is a syntax error before > the left brace of the body of onintr. That's because there is a syntax error before the left brace of the body of onintr. The signature should be something like: void onintr(int val) Here is a little tutorial: http://www.cs.utk.edu/~huangj/cs360/360/notes/Signals/lecture.html > The compiler also doesn't like the > original exit(err == YES) and I wrote > > if (err == YES) exit(1); > return; > > as a way to appease it. You should try to understand the compiler warnings. > -- > Ignorantly, > Allan Adler <a...(a)zurich.csail.mit.edu> > * Disclaimer: I am a guest and *not* a member of the MIT CSAIL. My actions and > * comments do not reflect in any way on MIT. Also, I am nowhere near Boston.
From: Allan Adler on 26 Feb 2007 12:57 "user923005" <dcorbit(a)connx.com> writes: > On Feb 26, 8:34 am, Allan Adler <a...(a)nestle.csail.mit.edu> wrote: > > Can someone please tell me what is wrong with the following program and > > how to fix it? It is running on a PC running RedHat 7.1 Linux. > > #include <signal.h> > > int err; void (*onintr)(int); > > #define YES 1 > > int main() > > {if (signal(SIGINT,SIG_IGN) != SIG_IGN) {signal(SIGINT,onintr);} return 0;} > > void (*onintr)(int){signal(SIGINT, onintr);if (err == YES) exit(1); > > /* exit(err == YES);*/ return;} > > The compiler (gcc 2.96) complains that there is a syntax error before > > the left brace of the body of onintr. > > That's because there is a syntax error before the left brace of the > body of onintr.> The signature should be something like: > void onintr(int val) Thanks. I was confused by the man page for signal, which gave the declaration: void (*signal(int signum, void (*sighandler)(int)))(int); so I just ASSUMED that that meant that the signal handler had to have the type void (*signalhandler)(int). > > The compiler also doesn't like the > > original exit(err == YES) and I wrote > > if (err == YES) exit(1); > > return; > > as a way to appease it. > > You should try to understand the compiler warnings. I spent a lot of time trying to understand the compiler warnings and wasn't successful. That's why I asked here. Appeasing the compiler in the manner I described above made it possible to eliminate the exit as the main problem. What method did you have in mind for understanding the compiler warnings and how would it have worked in the present instance? -- Ignorantly, Allan Adler <a...(a)zurich.csail.mit.edu> * Disclaimer: I am a guest and *not* a member of the MIT CSAIL. My actions and * comments do not reflect in any way on MIT. Also, I am nowhere near Boston.
From: user923005 on 26 Feb 2007 13:08 On Feb 26, 9:57 am, Allan Adler <a...(a)nestle.csail.mit.edu> wrote: > "user923005" <dcor...(a)connx.com> writes: > > On Feb 26, 8:34 am, Allan Adler <a...(a)nestle.csail.mit.edu> wrote: > > > Can someone please tell me what is wrong with the following program and > > > how to fix it? It is running on a PC running RedHat 7.1 Linux. > > > #include <signal.h> > > > int err; void (*onintr)(int); > > > #define YES 1 > > > int main() > > > {if (signal(SIGINT,SIG_IGN) != SIG_IGN) {signal(SIGINT,onintr);} return 0;} > > > void (*onintr)(int){signal(SIGINT, onintr);if (err == YES) exit(1); > > > /* exit(err == YES);*/ return;} > > > The compiler (gcc 2.96) complains that there is a syntax error before > > > the left brace of the body of onintr. > > > That's because there is a syntax error before the left brace of the > > body of onintr.> The signature should be something like: > > void onintr(int val) > > Thanks. I was confused by the man page for signal, which gave the > declaration: > void (*signal(int signum, void (*sighandler)(int)))(int); > > so I just ASSUMED that that meant that the signal handler had to have > the type void (*signalhandler)(int). You need to understand the difference between a typedef and a function prototype. They are not the same thing. > > > The compiler also doesn't like the > > > original exit(err == YES) and I wrote > > > if (err == YES) exit(1); > > > return; > > > as a way to appease it. > > > You should try to understand the compiler warnings. > > I spent a lot of time trying to understand the compiler warnings and > wasn't successful. That's why I asked here. Appeasing the compiler in > the manner I described above made it possible to eliminate the exit as > the main problem. > > What method did you have in mind for understanding the compiler warnings > and how would it have worked in the present instance? It appears to me that you are not a C programmer. May I suggest: http://en.wikipedia.org/wiki/The_C_Programming_Language_(book) I think you are going to have a terrible time of it if you try to implement something in a computer language that you do not understand. > -- > Ignorantly, > Allan Adler <a...(a)zurich.csail.mit.edu> > * Disclaimer: I am a guest and *not* a member of the MIT CSAIL. My actions and > * comments do not reflect in any way on MIT. Also, I am nowhere near Boston.
From: Jens Thoms Toerring on 26 Feb 2007 15:50 Allan Adler <ara(a)nestle.csail.mit.edu> wrote: > "user923005" <dcorbit(a)connx.com> writes: > > On Feb 26, 8:34 am, Allan Adler <a...(a)nestle.csail.mit.edu> wrote: > > > Can someone please tell me what is wrong with the following program and > > > how to fix it? It is running on a PC running RedHat 7.1 Linux. > > > #include <signal.h> > > > int err; void (*onintr)(int); > > > #define YES 1 > > > int main() > > > {if (signal(SIGINT,SIG_IGN) != SIG_IGN) {signal(SIGINT,onintr);} return 0;} > > > void (*onintr)(int){signal(SIGINT, onintr);if (err == YES) exit(1); > > > /* exit(err == YES);*/ return;} > > > The compiler (gcc 2.96) complains that there is a syntax error before > > > the left brace of the body of onintr. > > > > That's because there is a syntax error before the left brace of the > > body of onintr.> The signature should be something like: > > void onintr(int val) > Thanks. I was confused by the man page for signal, which gave the > declaration: > void (*signal(int signum, void (*sighandler)(int)))(int); > so I just ASSUMED that that meant that the signal handler had to have > the type void (*signalhandler)(int). If you add a semicolon that would define a variable (called 'signalhandler') that is a pointer to a function which takes a single int argument and returns void. And that's exactly what you need to pass as the second argument to signal() and also what the function signal() returns - the second argment is the pointer to the fucntion to be used from now on and the return value is a pointer to the function that was used before the call of signal(). To it that easier to read the prototype for signal() often is written like this: typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); That creates a new type called 'sighandler_t', which is a function pointer (taking an int and returning void). This is then used in the following declaration of signal(), which now looks a bit less daunting;-) BTW, if you're on a POSIX system I would recommend to use sigaction() instead of signal(). Regards, Jens -- \ Jens Thoms Toerring ___ jt(a)toerring.de \__________________________ http://toerring.de
|
Next
|
Last
Pages: 1 2 Prev: Bron-Kerbosh algorithm for clique detection Next: Directive error E3033? |