关于C中可变参数的记录

“有时候,在选择转载和原创时,自己迷惑了,多少知识都是别人嚼烂了啊”

——————————————————————————————————————

前言:

虽然可变参数的可以执行很差,受平台影响,cpu arch,大小端,栈增长等,不过有时候不考虑这些时,可以考虑用其完成一些技巧。

注:用#pragma print???


1. va_arg

va_arg(args, bool)
va_arg(args, char)
这两句运行正常,而在linux编译有note提示会有指令错误,运行时崩溃。


先看windows中vadefs.h的宏定义:

#elif   defined(_M_IX86)

#define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )

#define _crt_va_start(ap,v)  ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )
#define _crt_va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define _crt_va_end(ap)      ( ap = (va_list)0 )

linux的略去。

为何上面两句会在linux下有问题呢?呵呵,编译时还是好好看提示与警告信息

“ 'short unsigned int' is promoted to 'int' when passed through '...' ”

" 'char' is promoted to 'int' when passed through '...' " //传递char类型时会提升到int型






你可能感兴趣的:(c,可变参数)