当前位置:我的异常网» C语言 » DEFUN 如何定义支持不定长参数的函数

DEFUN 如何定义支持不定长参数的函数

www.myexceptions.net  网友分享于:2015-07-15  浏览:0次

DEFUN 怎么定义支持不定长参数的函数

DEFUN (modify_phyif_type,

modify_phyif_type_cmd,

"modify phyif type /*    网卡数   网卡名   类型   网卡名   类型   */",

"modify physical type\n"

".....\n"

".....\n")

{

return CMD_SUCCESS;

}

比如我想定义modify_phyif_type的一个函数,函数的参数不定长,由 "网卡数"这个参数决定。

参数占位符的问题:  如果是IP类型,可以用 (A.B.C.D) 表示

如果是字符串类型,可以用大写字母   (NAME) 表示

如果是数值类型,改用什么占位符?

------解决思路----------------------

int printf(const char *format, ...);

你可以参考一下prinft()函数

也可以定义一个结构体或指针

------解决思路----------------------

定义为数组试试

------解决思路----------------------

va_arg, va_end, va_start

Access variable-argument lists.

type va_arg( va_list arg_ptr, type );

void va_end( va_list arg_ptr );

void va_start( va_list arg_ptr );   (UNIX version)

void va_start( va_list arg_ptr, prev_param );   (ANSI version)

Routine Required Header Optional Headers Compatibility

va_arg  and  1 ANSI, Win 95, Win NT

va_end  and  1 ANSI, Win 95, Win NT

va_start  and  1 ANSI, Win 95, Win NT

1 Required for UNIX V compatibility.

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version

LIBCMT.LIB Multithread static library, retail version

MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

va_arg returns the current argument; va_start and va_end do not return values.

Parameters

type

Type of argument to be retrieved

arg_ptr

Pointer to list of arguments

prev_param

Parameter preceding first optional argument (ANSI only)

Remarks

The va_arg, va_end, and va_start macros provide a portable way to access the arguments to a function when the function takes a variable number of arguments. Two versions of the macros are available: The macros defined in STDARG.H conform to the ANSI C standard, and the macros defined in VARARGS.H are compatible with the UNIX System V definition. The macros are:

va_alist

Name of parameter to called function (UNIX version only)

va_arg

Macro to retrieve current argument

va_dcl

Declaration of va_alist (UNIX version only)

va_end

Macro to reset arg_ptr

va_list

typedef for pointer to list of arguments defined in STDIO.H

va_start

Macro to set arg_ptr to beginning of list of optional arguments (UNIX version only)

Both versions of the macros assume that the function takes a fixed number of required arguments, followed by a variable number of optional arguments. The required arguments are declared as ordinary parameters to the function and can be accessed through the parameter names. The optional arguments are accessed through the macros in STDARG.H or VARARGS.H, which set a pointer to the first optional argument in the argument list, retrieve arguments from the list, and reset the pointer when argument processing is completed.

The ANSI C standard macros, defined in STDARG.H, are used as follows:

All required arguments to the function are declared as parameters in the usual way. va_dcl is not used with the STDARG.H macros.

va_start sets arg_ptr to the first optional argument in the list of arguments passed to the function. The argument arg_ptr must have va_list type. The argument prev_param is the name of the required parameter immediately preceding the first optional argument in the argument list. If prev_param is declared with the register storage class, the macro’s behavior is undefined. va_start must be used before va_arg is used for the first time.

va_arg retrieves a value of type from the location given by arg_ptr and increments arg_ptr to point to the next argument in the list, using the size of type to determine where the next argument starts. va_arg can be used any number of times within the function to retrieve arguments from the list.

After all arguments have been retrieved, va_end resets the pointer to NULL.

The UNIX System V macros, defined in VARARGS.H, operate somewhat differently:

Any required arguments to the function can be declared as parameters in the usual way.

The last (or only) parameter to the function represents the list of optional arguments. This parameter must be named va_alist (not to be confused with va_list, which is defined as the type of va_alist).

va_dcl appears after the function definition and before the opening left brace of the function. This macro is defined as a complete declaration of the va_alist parameter, including the terminating semicolon; therefore, no semicolon should follow va_dcl.

Within the function, va_start sets arg_ptr to the beginning of the list of optional arguments passed to the function. va_start must be used before va_arg is used for the first time. The argument arg_ptr must have va_list type.

va_arg retrieves a value of type from the location given by arg_ptr and increments arg_ptr to point to the next argument in the list, using the size of type to determine where the next argument starts. va_arg can be used any number of times within the function to retrieve the arguments from the list.

After all arguments have been retrieved, va_end resets the pointer to NULL.

Example

/* VA.C: The program below illustrates passing a variable

* number of arguments using the following macros:

*      va_start            va_arg              va_end

*      va_list             va_dcl (UNIX only)

*/

#include

#define ANSI            /* Comment out for UNIX version     */

#ifdef ANSI             /* ANSI compatible version          */

#include

int average( int first, ... );

#else                   /* UNIX compatible version          */

#include

int average( va_list );

#endif

void main( void )

{

/* Call with 3 integers (-1 is used as terminator). */

printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );

/* Call with 4 integers. */

printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );

/* Call with just -1 terminator. */

printf( "Average is: %d\n", average( -1 ) );

}

/* Returns the average of a variable list of integers. */

#ifdef ANSI             /* ANSI compatible version    */

int average( int first, ... )

{

int count = 0, sum = 0, i = first;

va_list marker;

va_start( marker, first );     /* Initialize variable arguments. */

while( i != -1 )

{

sum += i;

count++;

i = va_arg( marker, int);

}

va_end( marker );              /* Reset variable arguments.      */

return( sum ? (sum / count) : 0 );

}

#else       /* UNIX compatible version must use old-style definition.  */

int average( va_alist )

va_dcl

{

int i, count, sum;

va_list marker;

va_start( marker );            /* Initialize variable arguments. */

for( sum = count = 0; (i = va_arg( marker, int)) != -1; count++ )

sum += i;

va_end( marker );              /* Reset variable arguments.      */

return( sum ? (sum / count) : 0 );

}

#endif

Output

Average is: 3

Average is: 8

Average is: 0

Argument Access Routines

See Also   vfprintf

------解决思路----------------------

Access variable-argument lists.

文章评论

c语言不定长参数函数,DEFUN 如何定义支持不定长参数的函数相关推荐

  1. 【c++】23.【函数指针】定义? 为什么不直接调用函数而要使用函数指针?

    1. 函数指针及其定义和用法,C语言函数指针详解 原文链接:http://c.biancheng.net/view/228.html 什么是函数指针 如果在程序中定义了一个函数,那么在编译时系统就会为 ...

  2. python内置函数调用_Python中函数的基本定义与调用及内置函数详解

    前言 函数function是python编程核心内容之一,也是比较重要的一块.首先我们要了解Python函数的基本定义: 函数是什么? 函数是可以实现一些特定功能的小方法或是小程序.在Python中有 ...

  3. python函数定义及调用-Python函数的基本定义和调用以及内置函数

    首先我们要了解Python函数的基本定义: 函数是什么? 函数是可以实现一些特定功能的小方法或是小程序.在Python中有很多内建函数,当然随着学习的深入,你也可以学会创建对自己有用的函数.简单的理解 ...

  4. c语言中inline用法,C语言陷阱与技巧第2节,使用inline函数可以提升程序效率,但是让inline函数生效是有条件的...

    打开 Linux 内核源代码,会发现内核在定义C语言函数时,有很多都带有 "inline"关键字,请看下图,那么这个关键字有什么作用呢? inline 关键字的作用 在C语言程序开 ...

  5. append函数_连载|想用Python做自动化测试?函数的参数传递机制及变量作用域

    " 这一节有点难.看不懂没关系.继续往后学,回头再来看." 10.6 函数参数传递的机制 10.6.1 值传递与引用传递 编程语言的参数传递机制通常有两种: 值传递 拷贝参数的值, ...

  6. 函数指针及其定义和用法

    函数指针及其定义和用法 1.什么是函数指针 如果在程序中定义了一个函数,那么在编译时系统就会为这个函数代码分配一段存储空间,这段存储空间的首地址称为这个函数的地址.而且函数名表示的就是这个地址.既然是 ...

  7. 鸿蒙TouchEvent已实现单击、连续、长按功能,安卓也可以模仿着原理实现(网上绝大多未实现不动长按),这个支持不动长按事件

    原创文章引用请注明出处,文章问题持续优化中. 以下的例子是模仿抖音的刷视频的单击播放.暂停,长按弹出框,连续点击是点赞的效果.连续点击视频时候,视频处于播放或者暂停是不会击穿事件导致视频播放或者暂停的 ...

  8. 关于C++模板函数声明与定义的问题

    关于C++模板函数声明与定义的问题 关于C++模板函数声明与定义的问题 模板函数出现的问题 模板函数问题解决 模板函数出现的问题 今天在写代码的时候,发现了一个关于模板函数的问题.如下所示, demo ...

  9. python3_函数_形参调用方式 / 不定长参数 / 函数返回值 / 变量作用域 / 匿名函数 / 递归调用 / 函数式编程 / 高阶函数 / gobal和nonlocal关键字 / 内置函数

    1.形参的调用方式 1. 位置参数调用 2. 关键词参数调用 原则: 关键词参数调用不能写在位置参数调用的前边 def test1(name, age):print("name:" ...

最新文章

  1. baidumaptrace.php,鹰眼Web API v2.0 | 百度地图API SDK
  2. oracle localhost连接
  3. 解决: 网站访问报错 AccessDenied (阿里云 OSS + CDN )
  4. Spring : Spring profile 实现多环境支持
  5. 阿里云ECS服务器 Centos7.2 使用 yum 安装 ansible 报错
  6. 研究生周报模板免费下载
  7. 在和弦上进行旋律创作(不断更新)
  8. Unity3D Terrain 变成粉色(紫色/洋红色)解决方案!
  9. 用计算机怎样搜wifi网,笔记本电脑搜索不到无线网络(Wifi)怎么办
  10. office 2019 word鼠标点击反应慢要等一下
  11. USYD悉尼大学DATA1002 OralExam 复习(可能会考的内容)
  12. 腾讯云大学大咖分享 | 自然语言处理技术(NLP)究竟能做些什么?
  13. 太原师范学院计算机考研率,太原师范学院怎么样(太原师范学院考研率)
  14. linux系统怎么装搜狗输入法_Linux之Ubuntu系统安装搜狗输入法
  15. linux 硬盘错误,linux – 硬盘读取错误……停止?
  16. 简练网软考知识点整理-蒙特卡洛模拟
  17. 手机用python画太阳花的代码_利用python绘制太阳花(蓝桥杯试题)
  18. zotero配合百度云同步的方法
  19. java字节流读取文件_字节流读取文件 java的几种IO流读取文件方式
  20. 南京航天航空大学计算机推免,南京航空航天大学计算机学院2016研究生推免办法...

热门文章

  1. python之helloworld
  2. 华夏银行招聘计算机笔试题,2019华夏银行招聘结构化面试试题及答案
  3. 全网最详细的Android Studio卸载、安装和启动教程
  4. spring学习--jdbcTemplate - 增删改
  5. linux 如何起服务,如何修改Linux的服务的开启和关闭
  6. Pow,Pos,Dpos共识机制比较
  7. java 常用的五大包
  8. linux设备模型之tty驱动架构分析,linux设备模型之uart驱动架构分析
  9. nginx发布PHP代码,nginx服务器配置返回php代码
  10. IDEA 控制台显示Run Dashboard