subject_17194.htm
来自「一些关于vc的问答」· HTM 代码 · 共 54 行 · 第 1/2 页
HTM
54 行
<p>
序号:17194 发表者:天花乱追 发表日期:2002-10-09 12:46:14
<br>主题:va_arg, va_end, va_start这几个宏大致是什吗意思啊?
<br>内容:看了半天msdn还是没看懂:((,e文太差了!!!<BR><BR>int average( int first, ... )<BR>{<BR> int count = 0, sum = 0, i = first;<BR> va_list marker;<BR><BR> va_start( marker, first ); /* Initialize variable arguments. */<BR> while( i != -1 )<BR> {<BR> sum += i;<BR> count++;<BR> i = va_arg( marker, int);<BR> }<BR> va_end( marker ); /* Reset variable arguments. */<BR> return( sum ? (sum / count) : 0 );<BR>}
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:阿鬼 回复日期:2002-10-09 12:58:50
<br>内容:Macro Use <BR>va_arg Retrieve argument from list (取得參數列表中的某個參數)<BR>va_end Reset pointer (重置參數列表)<BR>va_start Set pointer to beginning of argument list (初始化參數列表)<BR><BR>/***<BR>*stdarg.h - defines ANSI-style macros for variable argument functions<BR>*<BR>* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.<BR>*<BR>*Purpose:<BR>* This file defines ANSI-style macros for accessing arguments<BR>* of functions which take a variable number of arguments.<BR>* [ANSI]<BR>*<BR>* [Public]<BR>*<BR>****/<BR><BR>#if _MSC_VER > 1000<BR>#pragma once<BR>#endif<BR><BR>#ifndef _INC_STDARG<BR>#define _INC_STDARG<BR><BR>#if !defined(_WIN32) && !defined(_MAC)<BR>#error ERROR: Only Mac or Win32 targets supported!<BR>#endif<BR><BR><BR>#ifdef _MSC_VER<BR>/*<BR> * Currently, all MS C compilers for Win32 platforms default to 8 byte<BR> * alignment.<BR> */<BR>#pragma pack(push,8)<BR>#endif /* _MSC_VER */<BR><BR>#ifdef __cplusplus<BR>extern "C" {<BR>#endif<BR><BR><BR><BR>#ifndef _VA_LIST_DEFINED<BR>#ifdef _M_ALPHA<BR>typedef struct {<BR> char *a0; /* pointer to first homed integer argument */<BR> int offset; /* byte offset of next parameter */<BR>} va_list;<BR>#else<BR>typedef char * va_list;<BR>#endif<BR>#define _VA_LIST_DEFINED<BR>#endif<BR><BR>#ifdef _M_IX86<BR><BR><BR>#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR><BR>#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )<BR>#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )<BR>#define va_end(ap) ( ap = (va_list)0 )<BR><BR>#elif defined(_M_MRX000)<BR><BR><BR>/* Use these types and definitions if generating code for MIPS */<BR><BR>#define va_start(ap,v) ap = (va_list)&v + sizeof(v)<BR>#define va_end(list)<BR>#define va_arg(list, mode) ((mode *)(list =\<BR> (char *) ((((int)list + (__builtin_alignof(mode)<=4?3:7)) &\<BR> (__builtin_alignof(mode)<=4?-4:-8))+sizeof(mode))))[-1]<BR><BR>/* +++++++++++++++++++++++++++++++++++++++++++<BR> Because of parameter passing conventions in C:<BR> use mode=int for char, and short types<BR> use mode=double for float types<BR> use a pointer for array types<BR> +++++++++++++++++++++++++++++++++++++++++++ */<BR><BR><BR>#elif defined(_M_ALPHA)<BR><BR><BR>/* Use these types and definitions if generating code for ALPHA */<BR><BR>/*<BR> * The Alpha compiler supports two builtin functions that are used to<BR> * implement stdarg/varargs. The __builtin_va_start function is used<BR> * by va_start to initialize the data structure that locates the next<BR> * argument. The __builtin_isfloat function is used by va_arg to pick<BR> * which part of the home area a given register argument is stored in.<BR> * The home area is where up to six integer and/or six floating point<BR> * register arguments are stored down (so they can also be referenced<BR> * by a pointer like any arguments passed on the stack).<BR> */<BR><BR>extern void * __builtin_va_start(va_list, ...);<BR><BR>#ifdef _CFRONT<BR>#define __builtin_isfloat(a) __builtin_alignof(a)<BR>#endif<BR><BR>#define va_start(list, v) __builtin_va_start(list, v, 1)<BR>#define va_end(list)<BR>#define va_arg(list, mode) \<BR> ( *( ((list).offset += ((int)sizeof(mode) + 7) & -8) , \<BR> (mode *)((list).a0 + (list).offset - \<BR> ((__builtin_isfloat(mode) && (list).offset <= (6 * 8)) ? \<BR> (6 * 8) + 8 : ((int)sizeof(mode) + 7) & -8) \<BR> ) \<BR> ) \<BR> )<BR><BR>#elif defined(_M_PPC)<BR><BR>/* Microsoft C8 front end (used in Motorola Merged compiler) */<BR>/* bytes that a type occupies in the argument list */<BR>#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR>/* return 'ap' adjusted for type 't' in arglist */<BR>#define _ALIGNIT(ap,t) \<BR> ((((int)(ap))+(sizeof(t)<8?3:7)) & (sizeof(t)<8?~3:~7))<BR><BR>#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )<BR>#define va_arg(ap,t) ( *(t *)((ap = (char *) (_ALIGNIT(ap, t) + _INTSIZEOF(t))) - _INTSIZEOF(t)) )<BR>#define va_end(ap) ( ap = (va_list)0 )<BR><BR>#elif defined(_M_M68K)<BR>#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR><BR>#define va_start(ap,v) ( ap = (va_list)&v + (sizeof(v) < sizeof(int) ? sizeof(v) : _INTSIZEOF(v)) )<BR>#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )<BR>#define va_end(ap) ( ap = (va_list)0 )<BR><BR>#elif defined(_M_MPPC)<BR>#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR><BR>#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )<BR>#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )<BR>#define va_end(ap) ( ap = (va_list)0 )<BR><BR>#else<BR><BR>/* A guess at the proper definitions for other platforms */<BR><BR>#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR><BR>#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )<BR>#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )<BR>#define va_end(ap) ( ap = (va_list)0 )<BR><BR><BR>#endif<BR><BR><BR>#ifdef __cplusplus<BR>}<BR>#endif<BR><BR>#ifdef _MSC_VER<BR>#pragma pack(pop)<BR>#endif /* _MSC_VER */<BR><BR>#endif /* _INC_STDARG */<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:天花乱追 回复日期:2002-10-09 13:06:20
<br>内容:ghost,看了你的答复我更晕了(注释我看懂了:),别的看着有些费劲:(),如果不麻烦的话可否给个简单的例句说明一下,多谢了!!!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:FullMoon 回复日期:2002-10-09 13:08:24
<br>内容:用在参数个数不定的函数里,开始时不知道,照葫芦画瓢就行了,只要作一两个后就会明白。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:徐景周 回复日期:2002-10-09 13:15:29
<br>内容:当参数为变量时使用它们,va_start 必须用在va_arg之前,va_start 初始化参数链表指针,va_arg检索当前参数,并使指针加一到下一个参数位置,最后用va_end 来复位参数指针,使其为空。
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?