【补充】stdarg.h 运用

 

//哥教的
#include<iostream>
#include<stdarg.h>

using namespace std;

int Add(int iNum,...);

int main()
{

    cout<<Add(1,1)<<endl;

    cout<<Add(2,2,2)<<endl;

    cout<<Add(3,5,5,3)<<endl;

    return 0;
}



int Add(int iNum,...)
{
    int i = 0;

    va_list Temp = {0};    //typedef char *  va_list;    //一个char型的指针,存放一个地址

    int Result  = 0;    //存计算结果用

    va_start(Temp,iNum);//开始地址ap为参数iNum后面的那个参数所在的地址

    //#define va_start(ap,v)  ( ap = (va_list)&v + _INTSIZEOF(v) )   

   //va_start(Temp,iNum) 表示 Temp 的地址 = iNum的地址+4个字节,因为iNun是int型的

//#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )  
   //注意这里是ap向下移了t个字节,但是取出来的值是ap地址所对应的值。一定要看清楚
//#define va_end(ap) ( ap = (va_list)0 ) for(i=0;i<iNum;i++) { Result += va_arg(Temp,int); //取出Temp地址所对应的值,也就是第一个加数的值,
                       //并且Temp的地址下移4个字节(+4)【由于内存是从低地址到高地址,所以说下移】
} //循环iNum次,即完成Result叠加计算 va_end(Temp); //结束,将Temp 置为 0 ?? return Result; //返回Result }

 

//以下是stdarg.h头文件的内容

 

/*
 * stdarg.h
 *
 * Provides facilities for stepping through a list of function arguments of
 * an unknown number and type.
 *
 * NOTE: Gcc should provide stdarg.h, and I believe their version will work
 *       with crtdll. If necessary I think you can replace this with the GCC
 *       stdarg.h.
 *
 * Note that the type used in va_arg is supposed to match the actual type
 * *after default promotions*. Thus, va_arg (..., short) is not valid.
 *
 * This file is part of the Mingw32 package.
 *
 * Contributors:
 *  Created by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
 *
 *  THIS SOFTWARE IS NOT COPYRIGHTED
 *
 *  This source code is offered for use in the public domain. You may
 *  use, modify or distribute it freely.
 *
 *  This code is distributed in the hope that it will be useful but
 *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
 *  DISCLAMED. This includes but is not limited to warranties of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * $Revision: 1.2 $
 * $Author: noer $
 * $Date: 1998/10/10 00:51:16 $
 *
 */

#ifndef _STDARG_H_
#define _STDARG_H_

/*
 * Don't do any of this stuff for the resource compiler.
 */
#ifndef RC_INVOKED

/* 
 * I was told that Win NT likes this.
 */
#ifndef _VA_LIST_DEFINED
#define _VA_LIST_DEFINED
#endif

#ifndef    _VA_LIST
#define _VA_LIST
typedef char* va_list;
#endif


/*
 * Amount of space required in an argument list (ie. the stack) for an
 * argument of type t.
 */
#define __va_argsiz(t)    \
    (((sizeof(t) + sizeof(int) - 1) / sizeof(int)) * sizeof(int))


/*
 * Start variable argument list processing by setting AP to point to the
 * argument after pN.
 */
#ifdef    __GNUC__
/*
 * In GNU the stack is not necessarily arranged very neatly in order to
 * pack shorts and such into a smaller argument list. Fortunately a
 * neatly arranged version is available through the use of __builtin_next_arg.
 */
#define va_start(ap, pN)    \
    ((ap) = ((va_list) __builtin_next_arg(pN)))
#else
/*
 * For a simple minded compiler this should work (it works in GNU too for
 * vararg lists that don't follow shorts and such).
 */
#define va_start(ap, pN)    \
    ((ap) = ((va_list) (&pN) + __va_argsiz(pN)))
#endif


/*
 * End processing of variable argument list. In this case we do nothing.
 */
#define va_end(ap)    ((void)0)


/*
 * Increment ap to the next argument in the list while returing a
 * pointer to what ap pointed to first, which is of type t.
 *
 * We cast to void* and then to t* because this avoids a warning about
 * increasing the alignment requirement.
 */

#define va_arg(ap, t)                    \
     (((ap) = (ap) + __va_argsiz(t)),        \
      *((t*) (void*) ((ap) - __va_argsiz(t))))

#endif /* Not RC_INVOKED */

#endif /* not _STDARG_H_ */
View Code

 

posted @ 2013-11-01 22:24  Geekers  阅读(263)  评论(0编辑  收藏  举报