msdn的例子,运行不了。

toryhector 2006-04-27 04:16:45
OS:WINXP
DEV:VC6.0
======================================
signal
Sets interrupt signal handling.
void ( *signal( int sig, void (__cdecl *func) ( int sig [, int subcode ] )) ) ( int sig );
1.谁解释下这个函数声明,感觉像typedef样的。
2.怎么修改使得程序能够运行哦。

/* FPRESET.C: This program uses signal to set up a
* routine for handling floating-point errors.
*/

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <string.h>

jmp_buf mark; /* Address for long jump to jump to */
int fperr; /* Global error number */

void __cdecl fphandler( int sig, int num ); /* Prototypes */
void fpcheck( void );

void main( void )
{
double n1, n2, r;
int jmpret;
/* Unmask all floating-point exceptions. */
_control87( 0, _MCW_EM );
/* Set up floating-point error handler. The compiler
* will generate a warning because it expects
* signal-handling functions to take only one argument.
*/
if( signal( SIGFPE, fphandler ) == SIG_ERR )

{
fprintf( stderr, "Couldn't set SIGFPE\n" );
abort(); }

/* Save stack environment for return in case of error. First
* time through, jmpret is 0, so true conditional is executed.
* If an error occurs, jmpret will be set to -1 and false
* conditional will be executed.
*/
jmpret = setjmp( mark );
if( jmpret == 0 )
{
printf( "Test for invalid operation - " );
printf( "enter two numbers: " );
scanf( "%lf %lf", &n1, &n2 );
r = n1 / n2;
/* This won't be reached if error occurs. */
printf( "\n\n%4.3g / %4.3g = %4.3g\n", n1, n2, r );

r = n1 * n2;
/* This won't be reached if error occurs. */
printf( "\n\n%4.3g * %4.3g = %4.3g\n", n1, n2, r );
}
else
fpcheck();
}
/* fphandler handles SIGFPE (floating-point error) interrupt. Note
* that this prototype accepts two arguments and that the
* prototype for signal in the run-time library expects a signal
* handler to have only one argument.
*
* The second argument in this signal handler allows processing of
* _FPE_INVALID, _FPE_OVERFLOW, _FPE_UNDERFLOW, and
* _FPE_ZERODIVIDE, all of which are Microsoft-specific symbols
* that augment the information provided by SIGFPE. The compiler
* will generate a warning, which is harmless and expected.

*/
void fphandler( int sig, int num )
{
/* Set global for outside check since we don't want
* to do I/O in the handler.
*/
fperr = num;
/* Initialize floating-point package. */
_fpreset();
/* Restore calling environment and jump back to setjmp. Return
* -1 so that setjmp will return false for conditional test.
*/
longjmp( mark, -1 );
}
void fpcheck( void )
{
char fpstr[30];
switch( fperr )
{
case _FPE_INVALID:
strcpy( fpstr, "Invalid number" );
break;
case _FPE_OVERFLOW:
strcpy( fpstr, "Overflow" );

break;
case _FPE_UNDERFLOW:
strcpy( fpstr, "Underflow" );
break;
case _FPE_ZERODIVIDE:
strcpy( fpstr, "Divide by zero" );
break;
default:
strcpy( fpstr, "Other floating point error" );
break;
}
printf( "Error %d: %s\n", fperr, fpstr );
}


Output

Test for invalid operation - enter two numbers: 5 0
Error 131: Divide by zero

...全文
260 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
toryhector 2006-04-28
  • 打赏
  • 举报
回复
1.你能告诉我下你怎么会想到MSDN的帮助是错误的,你是怎么调式出来的啊?
2.改了后,输入5 0结果不报error 131
starblade 2006-04-27
  • 打赏
  • 举报
回复
支持tfq2002,这是一个函数指针的声明。
bombwang 2006-04-27
  • 打赏
  • 举报
回复
学习ing
jixingzhong 2006-04-27
  • 打赏
  • 举报
回复
void ( *signal( int sig, void (__cdecl *func) ( int sig [, int subcode ] )) ) ( int sig );
1.谁解释下这个函数声明,感觉像typedef样的。
========================
声明一个函数 signal, 该函数返回一个函数指针,
(返回的函数指针指向的函数的参数是一个 int),
signal 函数参数有两个,
一个 int,
一个函数指针(这个指针指向的函数参数有一个int 或者 两个 int)
(不知道这里的 参数 int sig [, int subcode ] 理解对了没有, 呵呵)
tfq2002 2006-04-27
  • 打赏
  • 举报
回复
1:
void ( *signal( int sig, void (__cdecl *func) ( int sig [, int subcode ] )) ) ( int sig );的定义写错了,应该是:
_CRTIMP void (__cdecl * __cdecl signal(int, void (__cdecl *)(int)))(int);
意思是signal这个函数有两个参数,一个是int类型,另一个参数是指向void (__cdecl *)(int)这种函数的指针,而signal又返回一个指向函数的指针,最后的int是它的参数类型

2:
只要把fphandler函数的声明修改如下,即可:
void __cdecl fphandler( int num );

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧