读书人

求指点C语言中可变参数宏的知识

发布时间: 2014-01-12 00:03:16 作者: rapoo

求指导C语言中可变参数宏的知识
void va_test(char* a, char* b, char* c, …)//省略了一些代码
va_start(ap, c);//此时ap应该指向c后面的第一个可变参数,为什么我用printf("%s", ap);输出不了,ap不是已经指向那个可变参数了吗?求详解!!!
#define va_start(ap, v) (ap = (va_list)&(v) + _INTSIZEOF(v))
&v对这个函数而言不就是二级指针吗?为什么用到二级指针,直接去v的地址加上v的内存大小不照样得到v后面一个参数的地址吗?而用二级指针的话我调试了还不是那个地址,疑惑!
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) ) 这句前面*(t *)有什么意义?先强制转成二级指针再转为一级指针的作用是什么?
[解决办法]

void va_test(char* a, char* b, char* c, …){
//1) //定义va_list指针(不定参数列表指针)
va_list vlist;
//2)//初始化va_list指针,(不定参数指针,让他指向最后一个确定参数)
va_start(vlist,c);
//3)//读取后面的各个函数参数,格式为,type x = va_arg(vlist,type) ; 例如:
int x =
va_arg(vlist,int);

//4) 结束使用不定参数函数参数
va_end(vlist);
}

PS:
这4个步骤,缺一不可。。。。哦,va_end();也许作用不大。

[解决办法]
更正以下
//2)初始化va_list指针,(不定参数指针,让他指向最后一个确定参数的下一个参数)

va_start(vlist,c);其实就是让 vlist指向堆栈中,c后面一个参数。

[解决办法]
计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构……

对学习编程者的忠告:
眼过千遍不如手过一遍!
书看千行不如手敲一行!
手敲千行不如单步一行!
单步源代码千行不如单步对应汇编一行!

VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。
对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。
(Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)

[解决办法]
引用:
va_start(ap, c);//此时ap应该指向c后面的第一个可变参数,为什么我用printf("%s", ap);输出不了,ap不是已经指向那个可变参数了吗?

不是的, va_start 之后, ap 是指向第一个可变参数的地址,而不是第一个可变参数! 它需要解引用:
&v对这个函数而言不就是二级指针吗?为什么用到二级指针,直接去v的地址加上v的内存大小不照样得到v后面一个参数的地址吗?而用二级指针的话我调试了还不是那个地址,疑惑!

之所以会是二级指针只是因为你的参数本身是个指针而已,这几个宏是通用的宏,当你的参数不是指针时,它就只是个指针,不是二级指针了。
这里所做的事情正是 “去v的地址加上v的内存大小不照样得到v后面一个参数的地址”,v 本身是个指针,取它的地址就是二级指针。

引用:
这句前面*(t *)有什么意义?先强制转成二级指针再转为一级指针的作用是什么?

这其实就是进行一次解引用,并转换成 t 的类型, 不要再纠结几级指针了,先把 t 写成 int 类型来推导一下过程吧。
[解决办法]
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) ) 这句前面*(t *)有什么意义?先强制转成二级指针再转为一级指针的作用是什么?

这么没有那么多道道。

1)va_list 就是char *
typedef char *  va_list;
所以ap 是个 char 类型的指针
2)char 类型的指针,加上_INTSIZEOF(t)后,就指向下一个参数了。
注意这里是 +=,所以ap变了
 ap += _INTSIZEOF(t)
//让 ap跳过当前参数,指向下一个参数。
3)再减去_INTSIZEOF(t),回到当前参数的位置,不过ap现在,指向下一个参数。
注意这里是 - 所以ap不变。
(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t))
//跳过当前参数后,
//表达式
(ap += _INTSIZEOF(t)) - _INTSIZEOF(t)
的值,为
ap - _INTSIZEOF(t)

//
ap-_INTSIZEOF(t)
是当前参数的地址,类型为
char *

(t *)(ap-_INTSIZEOF(t))
char *
强制转换为
 t *

4)指针的强制类型转换,和指针解引用
 *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t))

强制类型转换后的得到的 一个t* (t 类型的指针),
指向的数据类型为 t ,解引用得到一个t类型的参数,是个左值。

5)总结,这里分几个步骤
5.1) 步骤1,前进到下一个参数,ap指向下一个参数。
ap += _INTSIZEOF(t) //让ap,加上认定的参数长度(t 类型的参数,在传递时候的长度),

5.2)步骤2,返回到当前参数 ,ap 不变,依然指向下一个参数。
(t*)(ap-_INTSIZEOF(t)) //这就是当前参数地址,这里看作t类型的参数地址, 


5.3)步骤3,强制类型转换,并解引用,最外面的括号,是为了防止宏扩展错误。
 (*(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)))

PS:
C 式的,带有不定类型和个数参数的,
函数的参数表中的,
省略号表示的,不确定参数表部分,
参数类型信息,无法直接传递,
只能,从已经确定的参数里,获取类型信息,
比如 scanf,printf 这两个函数,从第一个参数里,提取其他参数的类型信息。
或者,直接认定,是某种类型的参数。
比如,你做一个累加和的函数,第一个参数为整型,是参数个数
默认,其他所有参数的类型,都是整型,或者浮点类型。



[解决办法]
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 <stdio.h> and <stdarg.h> <varargs.h>1 ANSI, Win 95, Win NT
va_end <stdio.h> and <stdarg.h> <varargs.h>1 ANSI, Win 95, Win NT
va_start <stdio.h> and <stdarg.h> <varargs.h>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 <stdio.h>
#define ANSI /* Comment out for UNIX version */
#ifdef ANSI /* ANSI compatible version */
#include <stdarg.h>
int average( int first, ... );
#else /* UNIX compatible version */
#include <varargs.h>
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

读书人网 >C语言

热点推荐