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>&nbsp;&nbsp; int count = 0, sum = 0, i = first;<BR>&nbsp;&nbsp; va_list marker;<BR><BR>&nbsp;&nbsp; va_start( marker, first );&nbsp;&nbsp;&nbsp;&nbsp; /* Initialize variable arguments. */<BR>&nbsp;&nbsp; while( i != -1 )<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum += i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = va_arg( marker, int);<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; va_end( marker );&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Reset variable arguments.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<BR>&nbsp;&nbsp; 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&nbsp;&nbsp;&nbsp;&nbsp;Use <BR>va_arg&nbsp;&nbsp; Retrieve argument from list (取得參數列表中的某個參數)<BR>va_end&nbsp;&nbsp; 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>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.<BR>*<BR>*Purpose:<BR>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This file defines ANSI-style macros for accessing arguments<BR>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; of functions which take a variable number of arguments.<BR>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ANSI]<BR>*<BR>*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Public]<BR>*<BR>****/<BR><BR>#if&nbsp;&nbsp;&nbsp;&nbsp; _MSC_VER &gt; 1000<BR>#pragma once<BR>#endif<BR><BR>#ifndef _INC_STDARG<BR>#define _INC_STDARG<BR><BR>#if&nbsp;&nbsp;&nbsp;&nbsp; !defined(_WIN32) && !defined(_MAC)<BR>#error ERROR: Only Mac or Win32 targets supported!<BR>#endif<BR><BR><BR>#ifdef&nbsp;&nbsp;_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&nbsp;&nbsp;/* _MSC_VER */<BR><BR>#ifdef&nbsp;&nbsp;__cplusplus<BR>extern "C" {<BR>#endif<BR><BR><BR><BR>#ifndef _VA_LIST_DEFINED<BR>#ifdef&nbsp;&nbsp;_M_ALPHA<BR>typedef struct {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char *a0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* pointer to first homed integer argument */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int offset;&nbsp;&nbsp;&nbsp;&nbsp; /* byte offset of next parameter */<BR>} va_list;<BR>#else<BR>typedef char *&nbsp;&nbsp;va_list;<BR>#endif<BR>#define _VA_LIST_DEFINED<BR>#endif<BR><BR>#ifdef&nbsp;&nbsp;_M_IX86<BR><BR><BR>#define _INTSIZEOF(n)&nbsp;&nbsp; ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR><BR>#define va_start(ap,v)&nbsp;&nbsp;( ap = (va_list)&v + _INTSIZEOF(v) )<BR>#define va_arg(ap,t)&nbsp;&nbsp;&nbsp;&nbsp;( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )<BR>#define va_end(ap)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( ap = (va_list)0 )<BR><BR>#elif&nbsp;&nbsp; defined(_M_MRX000)<BR><BR><BR>/* Use these types and definitions if generating code for MIPS */<BR><BR>#define va_start(ap,v) ap&nbsp;&nbsp;= (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)&lt;=4?3:7)) &\<BR> (__builtin_alignof(mode)&lt;=4?-4:-8))+sizeof(mode))))[-1]<BR><BR>/*&nbsp;&nbsp;+++++++++++++++++++++++++++++++++++++++++++<BR>&nbsp;&nbsp;&nbsp;&nbsp;Because of parameter passing conventions in C:<BR>&nbsp;&nbsp;&nbsp;&nbsp;use mode=int for char, and short types<BR>&nbsp;&nbsp;&nbsp;&nbsp;use mode=double for float types<BR>&nbsp;&nbsp;&nbsp;&nbsp;use a pointer for array types<BR>&nbsp;&nbsp;&nbsp;&nbsp;+++++++++++++++++++++++++++++++++++++++++++ */<BR><BR><BR>#elif&nbsp;&nbsp; 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.&nbsp;&nbsp;The __builtin_va_start function is used<BR> * by va_start to initialize the data structure that locates the next<BR> * argument.&nbsp;&nbsp;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&nbsp;&nbsp;_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>&nbsp;&nbsp;&nbsp;&nbsp;( *(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((list).offset += ((int)sizeof(mode) + 7) & -8) , \<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(mode *)((list).a0 + (list).offset - \<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((__builtin_isfloat(mode) && (list).offset &lt;= (6 * 8)) ? \<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(6 * 8) + 8 : ((int)sizeof(mode) + 7) & -8) \<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;) \<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) \<BR>&nbsp;&nbsp;&nbsp;&nbsp;)<BR><BR>#elif&nbsp;&nbsp; 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)&nbsp;&nbsp; ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR>/* return 'ap' adjusted for type 't' in arglist */<BR>#define _ALIGNIT(ap,t) \<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((((int)(ap))+(sizeof(t)&lt;8?3:7)) & (sizeof(t)&lt;8?~3:~7))<BR><BR>#define va_start(ap,v)&nbsp;&nbsp;( ap = (va_list)&v + _INTSIZEOF(v) )<BR>#define va_arg(ap,t)&nbsp;&nbsp;&nbsp;&nbsp;( *(t *)((ap = (char *) (_ALIGNIT(ap, t) + _INTSIZEOF(t))) - _INTSIZEOF(t)) )<BR>#define va_end(ap)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( ap = (va_list)0 )<BR><BR>#elif&nbsp;&nbsp; defined(_M_M68K)<BR>#define _INTSIZEOF(n)&nbsp;&nbsp; ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR><BR>#define va_start(ap,v)&nbsp;&nbsp;( ap = (va_list)&v + (sizeof(v) &lt; sizeof(int) ? sizeof(v) : _INTSIZEOF(v)) )<BR>#define va_arg(ap,t)&nbsp;&nbsp;&nbsp;&nbsp;( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )<BR>#define va_end(ap)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( ap = (va_list)0 )<BR><BR>#elif&nbsp;&nbsp; defined(_M_MPPC)<BR>#define _INTSIZEOF(n)&nbsp;&nbsp; ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR><BR>#define va_start(ap,v)&nbsp;&nbsp;( ap = (va_list)&v + _INTSIZEOF(v) )<BR>#define va_arg(ap,t)&nbsp;&nbsp;&nbsp;&nbsp;( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )<BR>#define va_end(ap)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( ap = (va_list)0 )<BR><BR>#else<BR><BR>/* A guess at the proper definitions for other platforms */<BR><BR>#define _INTSIZEOF(n)&nbsp;&nbsp; ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )<BR><BR>#define va_start(ap,v)&nbsp;&nbsp;( ap = (va_list)&v + _INTSIZEOF(v) )<BR>#define va_arg(ap,t)&nbsp;&nbsp;&nbsp;&nbsp;( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )<BR>#define va_end(ap)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( ap = (va_list)0 )<BR><BR><BR>#endif<BR><BR><BR>#ifdef&nbsp;&nbsp;__cplusplus<BR>}<BR>#endif<BR><BR>#ifdef&nbsp;&nbsp;_MSC_VER<BR>#pragma pack(pop)<BR>#endif&nbsp;&nbsp;/* _MSC_VER */<BR><BR>#endif&nbsp;&nbsp;/* _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 + -
显示快捷键?