当前位置: 首页 > news >正文

Linux / Unix Command: va_arg

为什么80%的码农都做不了架构师?>>>   hot3.png

NAME

stdarg - variable argument lists  

SYNOPSIS

#include <stdarg.h>

void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src);  

DESCRIPTION

A function may be called with a varying number of arguments of varying types. The include file stdarg.h declares a type va_list and defines three macros for stepping through a list of arguments whose number and types are not known to the called function.

The called function must declare an object of typeva_list which is used by the macrosva_start,va_arg, andva_end

va_start

The va_start macro initializes ap for subsequent use by va_arg and va_end, and must be called first.

The parameterlast is the name of the last parameter before the variable argument list, i.e., the last parameter of which the calling function knows the type.

Because the address of this parameter may be used in theva_start macro, it should not be declared as a register variable, or as a function or an array type. 

va_arg

The va_arg macro expands to an expression that has the type and value of the next argument in the call. The parameter ap is the va_list ap initialized by va_start. Each call to va_arg modifies ap so that the next call returns the next argument. The parameter type is a type name specified so that the type of a pointer to an object that has the specified type can be obtained simply by adding a * to type.

The first use of theva_arg macro after that of the va_start macro returns the argument after last. Successive invocations return the values of the remaining arguments.

If there is no next argument, or iftype is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), random errors will occur.

Ifap is passed to a function that usesva_arg(ap,type) then the value ofap is undefined after the return of that function. 

va_end

Each invocation of va_start must be matched by a corresponding invocation of va_end in the same function. After the call va_end( ap ) the variable ap is undefined. Multiple transversals of the list, each bracketed by va_start and va_end are possible. va_end may be a macro or a function.  

va_copy

An obvious implementation would have a va_list a pointer to the stack frame of the variadic function. In such a setup (by far the most common) there seems nothing against an assignment

va_list aq = ap;
Unfortunately, there are also systems that make it an array of pointers (of length 1), and there one needs

va_list aq;
        *aq = *ap;
Finally, on systems where parameters are passed in registers, it may be necessary for va_start to allocate memory, store the parameters there, and also an indication of which parameter is next, so that va_arg can step through the list. Now va_end can free the allocated memory again. To accommodate this situation, C99 adds a macro va_copy, so that the above assignment can be replaced by

va_list aq;
        va_copy(aq, ap);
        ...
        va_end(aq);
Each invocation of va_copy must be matched by a corresponding invocation of va_end in the same function. Some systems that do not supply va_copy have __va_copy instead, since that was the name used in the draft proposal.  

EXAMPLES

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, *p, *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);
}
 

CONFORMING TO

The va_start, va_arg, and va_end macros conform to ANSI X3.159-1989 (``C89''). C99 defines the va_copy macro.  

Important: Use the man command (% man) to see how a command is used on your particular computer.


转载于:https://my.oschina.net/qihh/blog/56672

相关文章:

  • Oracle的基本使用一
  • 鼠标浮上 图片边框变色
  • SQLmap工具介绍及其使用
  • 51cto,这是怎么回事?
  • jquery 选择时间(小时)区间(一)
  • 五大浏览器内核
  • C# DateTime 月第一天和最后一天 取法
  • getLayoutInflater().inflate .
  • 前端lvs+keepalived 后端 lnmp 群集 mysql主从+sersync
  • 做好一个高效的程序员吧
  • OSPF各种LSA的比较
  • 数据库热备容灾方案(上海某医院)
  • Cygwin sshd Apache sshd
  • 【046】◀▶ HTML 关键字
  • Eclipse is running in a JRE, but a JDK is required错误的解决
  • [译]前端离线指南(上)
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • ES6系列(二)变量的解构赋值
  • express如何解决request entity too large问题
  • java2019面试题北京
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • js
  • magento2项目上线注意事项
  • mongo索引构建
  • React as a UI Runtime(五、列表)
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • vue脚手架vue-cli
  • 阿里云Kubernetes容器服务上体验Knative
  • - 概述 - 《设计模式(极简c++版)》
  • 简析gRPC client 连接管理
  • 坑!为什么View.startAnimation不起作用?
  • 聊聊flink的BlobWriter
  • 巧用 TypeScript (一)
  • 如何用vue打造一个移动端音乐播放器
  • 微信小程序设置上一页数据
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 一道闭包题引发的思考
  • 一个完整Java Web项目背后的密码
  • 从如何停掉 Promise 链说起
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • ###项目技术发展史
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (C语言)fread与fwrite详解
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (数据结构)顺序表的定义
  • (译) 函数式 JS #1:简介
  • (转)EOS中账户、钱包和密钥的关系
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • .chm格式文件如何阅读
  • .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)...