stdarg.h

Posted jawide

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了stdarg.h相关的知识,希望对你有一定的参考价值。

standrad parameter

该头文件包含了一个变量类型和3个宏

变量类型

va_list

#ifndef _VA_LIST_DEFINED
#ifdef  _M_ALPHA
typedef struct {
        char *a0;       /* pointer to first homed integer argument */
        int offset;     /* byte offset of next parameter */
} va_list;
#else
typedef char *  va_list;
#endif

它通常是一个字符型指针

 

va_start ( va_list valist , int num )

用于初始化va_list型的变量,从左到右获取num个参数。

 

va_arg ( valist valist , type )

以type型返回当前valist指向的参数,然后让valist所指的地址向后移动sizeof(type)个字节

就相当于返回type型的一个参数,然后valist指向下一个参数

 

va_end ( va_list valist )

让va_list型的变量指向NULL

就相当于释放了va_list

 

头文件

  1 /***
  2 *stdarg.h - defines ANSI-style macros for variable argument functions
  3 *
  4 *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5 *
  6 *Purpose:
  7 *       This file defines ANSI-style macros for accessing arguments
  8 *       of functions which take a variable number of arguments.
  9 *       [ANSI]
 10 *
 11 *       [Public]
 12 *
 13 ****/
 14 
 15 #if     _MSC_VER > 1000
 16 #pragma once
 17 #endif
 18 
 19 #ifndef _INC_STDARG
 20 #define _INC_STDARG
 21 
 22 #if     !defined(_WIN32) && !defined(_MAC)
 23 #error ERROR: Only Mac or Win32 targets supported!
 24 #endif
 25 
 26 
 27 #ifdef  _MSC_VER
 28 /*
 29  * Currently, all MS C compilers for Win32 platforms default to 8 byte
 30  * alignment.
 31  */
 32 #pragma pack(push,8)
 33 #endif  /* _MSC_VER */
 34 
 35 #ifdef  __cplusplus
 36 extern "C" {
 37 #endif
 38 
 39 
 40 
 41 #ifndef _VA_LIST_DEFINED
 42 #ifdef  _M_ALPHA
 43 typedef struct {
 44         char *a0;       /* pointer to first homed integer argument */
 45         int offset;     /* byte offset of next parameter */
 46 } va_list;
 47 #else
 48 typedef char *  va_list;
 49 #endif
 50 #define _VA_LIST_DEFINED
 51 #endif
 52 
 53 #ifdef  _M_IX86
 54 
 55 
 56 #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
 57 
 58 #define va_start(ap,v)  ( ap = (va_list)&v + _INTSIZEOF(v) )
 59 #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
 60 #define va_end(ap)      ( ap = (va_list)0 )
 61 
 62 #elif   defined(_M_MRX000)
 63 
 64 
 65 /* Use these types and definitions if generating code for MIPS */
 66 
 67 #define va_start(ap,v) ap  = (va_list)&v + sizeof(v)
 68 #define va_end(list)
 69 #define va_arg(list, mode) ((mode *)(list = 70  (char *) ((((int)list + (__builtin_alignof(mode)<=4?3:7)) & 71  (__builtin_alignof(mode)<=4?-4:-8))+sizeof(mode))))[-1]
 72 
 73 /*  +++++++++++++++++++++++++++++++++++++++++++
 74     Because of parameter passing conventions in C:
 75     use mode=int for char, and short types
 76     use mode=double for float types
 77     use a pointer for array types
 78     +++++++++++++++++++++++++++++++++++++++++++ */
 79 
 80 
 81 #elif   defined(_M_ALPHA)
 82 
 83 
 84 /* Use these types and definitions if generating code for ALPHA */
 85 
 86 /*
 87  * The Alpha compiler supports two builtin functions that are used to
 88  * implement stdarg/varargs.  The __builtin_va_start function is used
 89  * by va_start to initialize the data structure that locates the next
 90  * argument.  The __builtin_isfloat function is used by va_arg to pick
 91  * which part of the home area a given register argument is stored in.
 92  * The home area is where up to six integer and/or six floating point
 93  * register arguments are stored down (so they can also be referenced
 94  * by a pointer like any arguments passed on the stack).
 95  */
 96 
 97 extern void * __builtin_va_start(va_list, ...);
 98 
 99 #ifdef  _CFRONT
100 #define __builtin_isfloat(a) __builtin_alignof(a)
101 #endif
102 
103 #define va_start(list, v) __builtin_va_start(list, v, 1)
104 #define va_end(list)
105 #define va_arg(list, mode) 106     ( *(        ((list).offset += ((int)sizeof(mode) + 7) & -8) , 107         (mode *)((list).a0 + (list).offset - 108                     ((__builtin_isfloat(mode) && (list).offset <= (6 * 8)) ? 109                         (6 * 8) + 8 : ((int)sizeof(mode) + 7) & -8) 110                 ) 111        ) 112     )
113 
114 #elif   defined(_M_PPC)
115 
116 /* Microsoft C8 front end (used in Motorola Merged compiler) */
117 /* bytes that a type occupies in the argument list */
118 #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
119 /* return ‘ap‘ adjusted for type ‘t‘ in arglist */
120 #define _ALIGNIT(ap,t) 121         ((((int)(ap))+(sizeof(t)<8?3:7)) & (sizeof(t)<8?~3:~7))
122 
123 #define va_start(ap,v)  ( ap = (va_list)&v + _INTSIZEOF(v) )
124 #define va_arg(ap,t)    ( *(t *)((ap = (char *) (_ALIGNIT(ap, t) + _INTSIZEOF(t))) - _INTSIZEOF(t)) )
125 #define va_end(ap)      ( ap = (va_list)0 )
126 
127 #elif   defined(_M_M68K)
128 #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
129 
130 #define va_start(ap,v)  ( ap = (va_list)&v + (sizeof(v) < sizeof(int) ? sizeof(v) : _INTSIZEOF(v)) )
131 #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
132 #define va_end(ap)      ( ap = (va_list)0 )
133 
134 #elif   defined(_M_MPPC)
135 #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
136 
137 #define va_start(ap,v)  ( ap = (va_list)&v + _INTSIZEOF(v) )
138 #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
139 #define va_end(ap)      ( ap = (va_list)0 )
140 
141 #else
142 
143 /* A guess at the proper definitions for other platforms */
144 
145 #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
146 
147 #define va_start(ap,v)  ( ap = (va_list)&v + _INTSIZEOF(v) )
148 #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
149 #define va_end(ap)      ( ap = (va_list)0 )
150 
151 
152 #endif
153 
154 
155 #ifdef  __cplusplus
156 }
157 #endif
158 
159 #ifdef  _MSC_VER
160 #pragma pack(pop)
161 #endif  /* _MSC_VER */
162 
163 #endif  /* _INC_STDARG */

 

  

以上是关于stdarg.h的主要内容,如果未能解决你的问题,请参考以下文章

C stdarg.h

无法在 virtualenv 中安装 psycopg2 “错误:stdarg.h:没有这样的文件或目录”

stdarg

关于可变参数列表stdarg

C语言怎么实现可变参数?

C语言函数参数中的三个点(三点 “...”)是干什么用的?(可变参数)<stdarg.h>va_start 宏va_arg 宏va_end 宏