在C/Objc中 写一个参数个数可变的函数示例

//
//  main.c
//  UncertainParametersAndParameterSentinel
//
//  Created by llc on 15/2/12.
//  Copyright (c) 2015年 llc. All rights reserved.
//

#include <stdio.h>
#include <stdarg.h>

int addemUp(int firstNum,...);
void printStrings(char * first,...) __attribute__((sentinel));

int main(int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    int sumbody0 = addemUp(1,2,3,4,5,6,7,8,9,0);
    printf("sum of 1...9 is %d \n",sumbody0);
    int sumbody1 = addemUp(1,2,3,0);
    printf("sum of 1,2,3 is %d \n",sumbody1);
    int sumbody2 = addemUp(1,2,3,4,5,6,7,8,9,10,11,0);
    printf("sum of 1...11 is %d \n",sumbody2);
    
    printStrings(" spicy"," pony"," head",NULL);
    printStrings("machine ","tool", NULL);
    return 0;
}

/* In C and Objective-C , How to write a function with variable number of arguments  such as printf ,Can receive a plurality of input parameters。
A reliable solution is to use the functions provided by the stdarg.h. Examples are as follows:      */

int addemUp(int firstNum,...){
 va_list args;
 int sum = firstNum;
 int number;
 va_start(args, firstNum);
 while (1) {
 number = va_arg(args, int);
 sum += number;
 if (number == 0) {
 break;
 }
 }
 va_end(args);
 return sum;
 }

/*
 Among them, the key functions are va_start (), va_arg (), va_end (), you also need to declare a variable of type va_list to store all parameters. Program to 0 for the identity, said that it has no parameters.
 */

/*
So Sentinel arguments came out. In the example above, the last parameter 0 is a sentinel parameter, its role is to tell the function has no parameters. This is why ObjC in [NSArray arrayWithObjects: ..., nil] finally have reason to nil at the end of the parameter.
Use __attribute __ ((sentinel)) syntax can tell the compiler which is a function of a need for sentinel parameters, such as:
 */
void printStrings(char * first,...){
    va_list args;
    va_start(args, first);
    char * string = first;
    while (string != NULL) {
        printf("%s",string);
        string = va_arg(args, char *);
    }
    va_end(args);
    printf("\n");
}

/*NS_REQUIRES_NIL_TERMINATION Cocoa programmers can use to get the same effect:
 - (Void) functionName: (id) memb1, ... NS_REQUIRES_NIL_TERMINATION;
 Represents the last parameter to nil at the end.*/

时间: 2024-08-11 12:47:15

在C/Objc中 写一个参数个数可变的函数示例的相关文章

C中参数个数可变的函数

一.什么是可变参数 我们在C语言编程中有时会遇到一些参数个数可变的函数,例如printf()函数,其函数原型为: int printf( const char* format, ...); 它除了有一个参数format固定以外,后面跟的参数的个数和类型是可变的(用三个点"-"做参数占位符),实际调用时可以有以下的形式: printf("%d",i); printf("%s",s); printf("the number is %d ,s

Javascript参数个数可变的函数 动态传人和取得参数

js中的函数是一个对象,一个Function对象(函数参数列表及函数主体事实上只是Function对象的构造函数的参数而已),跟其他对象一 样可以在运行时增加属性.删除属性,函数参数是可变的,比如定义函数时定义的参数列表只有3个参数,调用时你却可以只传2个参数,或超过3个参数,通过函 数对象的length属性可以得到函数定义的形参个数,而函数调用时的实际参数列表则可以通过函数的arguments属性(一个Arguments对 象,类似于数组)访问到,arguments.length就是实际参数的

JavaScript参数个数可变的函数

题外话:很早就接触了JavaScript,但是没有留意它,看到很多很酷.很炫的网页,都有JavaScript的身影,Google在JavaScript的应用对我的影响最大了.我决心从头开始学习它,所以有了JavaScript & Ajax这个分栏.我打算把这个分栏作为学习笔记记录下来,因此每篇文章笔记可能很简短,一两句话注解而已. JavaScript允许一个函数传递个数可变的参数,因为有arguments这个内置对象,它一个函数传递的所有参数的数组.举个例子,就明白了. 可以精心开发5年的UI

参数个数可变的函数

使用va_start(),va_arg(),va_end(),操作了可变元 定义如下: #include<stdarg.h> type va_arg(va_list argptr,type); void va_start(va_list argptr,last_parm); void va_end(va_list argptr); 用法: 1 #include<stdio.h> 2 #include<stdarg.h> 3 4 double sum_series(int

【转载】C语言 构建参数个数不固定函数

深入浅出可变参数函数的使用技巧本文主要介绍可变参数的函数使用,然后分析它的原理,程序员自己如何对它们实现和封装,最后是可能会出现的问题和避免措施. VA函数(variable argument function),参数个数可变函数,又称可变参数函数.C/C++编程中,系统提供给编程人员的va函数很少.*printf()/*scanf()系列函数,用于输入输出时格式化字符串:exec*()系列函数,用于在程序中执行外部文件(main(int argc,char*argv[]算不算呢,与其说main

Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载

Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载 这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数,需要的朋友可以参考下 $#代表了命令行的参数数量,可以看以下实例: 复制代码 代码如下: if [ $# != 1 ] ; then echo "USAGE: $0 TABNAME" echo " e.g.: $0 CDR_CALL_20040701" exit 1; f

怎样写参数个数可变的宏 Debug宏 Log宏等

编译器内置宏: 先介绍几个编译器内置的宏定义,这些宏定义不仅可以帮助我们完成跨平台的源码编写,灵活使用也可以巧妙地帮我们输出非常有用的调试信息. ANSI C标准中有几个标准预定义宏(也是常用的): __LINE__:在源代码中插入当前源代码行号: __FILE__:在源文件中插入当前源文件名: __DATE__:在源文件中插入当前的编译日期 __TIME__:在源文件中插入当前编译时间: __STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1: __cplusplus:当编写C

JS去掉url地址中的一个参数

在工作的过程中总遇到奇奇怪怪的问题,比如,我需要在打开某一个页面时,去掉地址栏中的一个参数,以下是一大神写给我的JS代码: <script> var currentUrl = window.location.href; var targetUrl = currentUrl.replace(/\/shi/, ""); window.location.href = targetUrl; </script> var targetUrl = currentUrl.rep

Javascript RegExp对象---获取url中某一个参数的值

RegExp 对象 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 直接量语法 /pattern/attributes实例:window.location.href:http://localhost:8100/aspx/main/ServiceCenter_list.aspx?category_id=93&page=2要匹配到的category_id=93:/category_id=\d+/g 创建 RegExp 对象的语法: new RegExp(pattern, at