variadic function 的使用

最近在看<the c programming language> K&R

  7.3章 Variable-length Argument Lists  变长参数列表, 笔记一下用法

1. 要用到的头文件  <stdarg.h>

  包含一些用来遍历(step through)变长参数列表的  宏(marco)定义

2. 类型 va_list : refer to each argument in turn    (va 是 variadic arguments的缩写)

  va_list ap;     /* 声明va_list 类型的指针 ap */

3. 宏va_start

  用法: va_start(va_list , char *);

  初始化 va_list 类型的指针, 指向列表中第一个未命名的参数, 一定要先调用它

  va_start 要用到最后一个命名的参数,所以函数参数列表中至少要有一个命名参数

4.va_arg

  用法: va_arg(va_list , type name);         /* type name 可能是 int double 这些类型 */

  返回一个参数, 将指针指向下一个参数

5.va_end

  用法: va_end(va_list);

  对va_start 的善后工作

6.va_cpy

  用法:  va_copy(va_list src, va_list dest);  

示例代码: (摘自: man page)

对va_list  va_start va_arg  va_end 的简单顺序调用

/*  The  function  foo  takes  a string of format characters and prints out the argument associated *     with each format character based on the type.  */       #include <stdio.h>
       #include <stdarg.h>

       void
       foo(char *fmt, ...)
       {
           va_list ap;
           int d;
           char c, *s;

           va_start(ap, fmt);      
           while (*fmt)
               switch (*fmt++) {
               case ‘s‘:              /* string */
                   s = va_arg(ap, char *);
                   printf("string %s\n", s);
                   break;
               case ‘d‘:              /* int */
                   d = va_arg(ap, int);
                   printf("int %d\n", d);
                   break;
               case ‘c‘:              /* char */
                   /* need a cast here since va_arg only
                      takes fully promoted types */
                   c = (char) va_arg(ap, int);
                   printf("char %c\n", c);
                   break;
               }
           va_end(ap);
       }

附: Linux下可以用man 命令查看 stdarg, va_start, va_arg, va_end, va_copy

  man 3 stdarg

时间: 2024-10-19 01:38:08

variadic function 的使用的相关文章

variadic function _ golang

Variadic functions can be called with any number of trailing arguments. For example, fmt.Println is a common variadic function package main import ( "fmt" ) func sum(nums ...int) { fmt.Println(nums, " ") total := 0 for _, num := range

可变参数的函数(variadic function)的陷阱

1,介绍variadic function 可变参数的函数就是参数数量可以改变的函数.例如printf(): int printf(const char *format, ...); printf("%d%s\n",i,s); C语言之所以可以支持可变参数函数,一个重要的原因是C调用规范中规定C语言函数调用时,参数是从右向左压入栈的:这样一个函数实现的时候,就无需关心调用他的函数会传递几个参数过来,而只要关心自己用到几个:例子: #include<stdarg.h> voi

Item 22: Use arguments to Create Variadic Functions

Item 22: Use arguments to Create Variadic FunctionsItem 21 describes a variadic average function, which can process anarbitrary number of arguments and produce their average value. Howcan we implement a variadic function of our own? The fixed-arity v

C++11 : variadic templates(可变参数模板)

Introduction: Before the possibilities of the new C++ language standard, C++11, the use of templates was quite limited when it came to implementing for instance function objects (functors) & tuple facilities. Implementing these sort of things using e

iOS -- warnings

Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belong to the input codeset UTF-8 -WNSObject-attribute         __attribute ((NSObject)) may be put on a typedef only, attribute is ignore

IOS 警告 收集

Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belong to the input codeset UTF-8 -WNSObject-attribute __attribute ((NSObject)) may be put on a typedef only, attribute is ignored -Wabst

[c++] Templates

Template是编译时多态.所有的模板都是在编译时产生对应的代码,它没有面向对象中的虚表,无法实现动态多态. Function Template A function template is a prescription for the compiler to generate particular instances of a function varying by type. ”变量类型“的部分放在一块,与“变量类型无关”的部分放在另一边. 放在"头文件“中,声明和实现建议这么做.其他方式

IOS 警告 汇总

Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belong to the input codeset UTF-8 -WNSObject-attribute __attribute ((NSObject)) may be put on a typedef only, attribute is ignored -Wabst

Parameter pack

Parameter pack C++ C++ language Templates A template parameter pack is a template parameter that accepts zero or more template arguments (non-types, types, or templates). A function parameter pack is a function parameter that accepts zero or more fun