主题:[讨论][求助]又是float的问题
下面这段代码在_TYPE为2的时候会运行错误,也就说在max函数中,如果type是float的时候,指针p是不能正确的访问到传递给max的参数。
[code=c]
#define _TYPE 2
#if _TYPE==1 /******int*****/
#define TYPE int
#elif _TYPE==2 /*****float****/
#define TYPE float
#elif _TYPE==3 /****double****/
#define TYPE double
#endif
TYPE LAST_ARG=(TYPE)-1; /****最后一个参数****/
//#include "stdlib.h"
#include "stdio.h"
#define my_assert(x) if(!(x)) do{ \
printf("L #%03d 断言失败\n\n",__LINE__); \
system("pause");exit(1); \
} while(0)
TYPE max( TYPE a,... )
{
TYPE *p =(TYPE*)(&a) ;
TYPE result = a ;
while( *++p != LAST_ARG )
*p>result ? result= *p : 0 ;
return result ;
} // max
int main( void )
{
#if (_TYPE==2) || (_TYPE==3)
TYPE a0=0.0, a1=1.0, a2=2.0, a3=3.0, a4=4.0,
a5=5.0, a6=6.0, a7=6.5, a8=8.5, a9=100.0 ;
#endif
#if (_TYPE==1) || (_TYPE==0)
my_assert( max(1,LAST_ARG)==1 );
my_assert( max(2,5,1,LAST_ARG)==5 );
my_assert( max(8,4,0,100,LAST_ARG)==100 );
#elif (_TYPE==2) || (_TYPE==3)
my_assert( max(a2,LAST_ARG)==a2 );
my_assert( max(a3,a7,a6,LAST_ARG)==a7 );
my_assert( max(a0,a4,a1,a9,LAST_ARG)==a9 );
#endif
printf( " 测试成功!!!\n\n");
system( "pause" );
return 0 ;
}[/code]
[code=c]
#define _TYPE 2
#if _TYPE==1 /******int*****/
#define TYPE int
#elif _TYPE==2 /*****float****/
#define TYPE float
#elif _TYPE==3 /****double****/
#define TYPE double
#endif
TYPE LAST_ARG=(TYPE)-1; /****最后一个参数****/
//#include "stdlib.h"
#include "stdio.h"
#define my_assert(x) if(!(x)) do{ \
printf("L #%03d 断言失败\n\n",__LINE__); \
system("pause");exit(1); \
} while(0)
TYPE max( TYPE a,... )
{
TYPE *p =(TYPE*)(&a) ;
TYPE result = a ;
while( *++p != LAST_ARG )
*p>result ? result= *p : 0 ;
return result ;
} // max
int main( void )
{
#if (_TYPE==2) || (_TYPE==3)
TYPE a0=0.0, a1=1.0, a2=2.0, a3=3.0, a4=4.0,
a5=5.0, a6=6.0, a7=6.5, a8=8.5, a9=100.0 ;
#endif
#if (_TYPE==1) || (_TYPE==0)
my_assert( max(1,LAST_ARG)==1 );
my_assert( max(2,5,1,LAST_ARG)==5 );
my_assert( max(8,4,0,100,LAST_ARG)==100 );
#elif (_TYPE==2) || (_TYPE==3)
my_assert( max(a2,LAST_ARG)==a2 );
my_assert( max(a3,a7,a6,LAST_ARG)==a7 );
my_assert( max(a0,a4,a1,a9,LAST_ARG)==a9 );
#endif
printf( " 测试成功!!!\n\n");
system( "pause" );
return 0 ;
}[/code]