您的位置:首页 > 编程语言

MTK优美代码赏析8:可变参数的C函数

2010-10-27 22:22 525 查看
对于可变参数函数,自己一直以来都很关注,尤其当自己刚才C#上转到C时,一度曾研究C上的面向对象,将类似printf的函数归类为面向对象里的函数多态,当时只是一个主观感受。今日仔细看了下代码的实现,发现使用C提供的公共接口是可以实现的,但这些接口是如何实现的就不知了。。。

文件:stdarg.h

gcc中的定义为:

/* Define the standard macros for the user,

if this invocation was from the user program. */

/* Define the standard macros for the user, if this invocation was from the user program. */

#define va_start(v,l) __builtin_va_start(v,l)

#define va_end(v) __builtin_va_end(v)

#define va_arg(v,l) __builtin_va_arg(v,l)

#if !defined(__STRICT_ANSI__) || __STDC_VERSION__ + 0 >= 199900L

#define va_copy(d,s) __builtin_va_copy(d,s)

#endif

下面看dbg_print(char *fmt,…)函数实现,部分内容摘自:《用库函数stdarg.h实现函数参数的可变》http://blog.sina.com.cn/s/blog_4ce3d23e0100efm3.html

代码

typedef int *va_list[1];

#define va_start(ap, parmN) (void)(*(ap) = __va_start(parmN))

#define va_arg(ap, type) __va_arg(*(ap), type)

#define va_copy(dest, src) ((void)(*(dest) = *(src)))

#define va_end(ap) ((void)(*(ap) = 0))

#undef tolower

#undef isdigit

#define isdigit(c) (((c) >= ’0′) && ((c) <= ’9′))

#define tolower(c) (((c)>=’A’ && (c)<=’Z')?((c)+0×20):(c))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: