zoukankan      html  css  js  c++  java
  • C语言 不确定参数个数的简单实现

    #include "stdafx.h"
    #include <iostream.h>

    /************************************************************************
    函数功能: 实现跟printf()函数一样,不确定参数个数,
             
    参数累加功能
       
    参数: 参数可以是N,但第后一个必须为-1来作为结束条件
    ************************************************************************/

    int OneAddToN(int nFirst, ...)
    {
        int nRet = 0;
        //
    得到第一个参数的地址
        int *pFirst = &nFirst;
       
        //
    参数值不为-1就循环累加
        while (-1 != *pFirst)
        {
            nRet += *pFirst;
            //
    移动指针,使其指向第2, 3, ..., N个参数
            pFirst++;
        }
        return nRet;
    }


    int main(int argc, char* argv[])
    {
        cout << OneAddToN(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1) << endl;
        return 0;
    }

    多参

  • 相关阅读:
    write to logfile
    open and read a file content to a variable
    strategy
    Android 开机启动程序
    消息队列
    卡机音乐功能实现
    Android 2.0 开机动画文件分析
    多线程实例
    消息队列
    多线程实例
  • 原文地址:https://www.cnblogs.com/w413133157/p/1652688.html
Copyright © 2011-2022 走看看