首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

function(LPCTSTR fmt, .)这个函数什么意思?该如何处理

2013-12-26 
function(LPCTSTR fmt, ...)这个函数什么意思?调用的时候直接是Cstring str;function(str);而str只是一个

function(LPCTSTR fmt, ...)这个函数什么意思?
调用的时候直接是
Cstring str;
function(str);而str只是一个参数,定义的时候的...是一种什么用法?
如果没区别我直接function(LPCTSTR fmt)不就好了嘛
[解决办法]
...是变参数,也就是说如果使用这样的定义 参数的数目是可变的 而如果你使用你说的方法 那么如果希望传入多个参数的时候 就不行了
[解决办法]
If at least one parameter occurs in the parameter list, the list can end with a comma followed by three periods (, ...). This construction, called the “ellipsis notation,” indicates a variable number of arguments to the function. (See Calls with a Variable Number of Arguments for more information.) However, a call to the function must have at least as many arguments as there are parameters before the last comma. 

Calls with a Variable Number of Arguments
A partial parameter list can be terminated by the ellipsis notation, a comma followed by three periods (, ...), to indicate that there may be more arguments passed to the function, but no more information is given about them. Type checking is not performed on such arguments. At least one parameter must precede the ellipsis notation and the ellipsis notation must be the last token in the parameter list. Without the ellipsis notation, the behavior of a function is undefined if it receives parameters in addition to those declared in the parameter list. 

To call a function with a variable number of arguments, simply specify any number of arguments in the function call. An example is the printf function from the C run-time library. The function call must include one argument for each type name declared in the parameter list or the list of argument types. 

All the arguments specified in the function call are placed on the stack unless the __fastcall calling convention is specified. The number of parameters declared for the function determines how many of the arguments are taken from the stack and assigned to the parameters. You are responsible for retrieving any additional arguments from the stack and for determining how many arguments are present. The STDARGS.H file contains ANSI-style macros for accessing arguments of functions which take a variable number of arguments. Also, the XENIX?- style macros in VARARGS.H are still supported.

This sample declaration is for a function that calls a variable number of arguments:  

int average( int first, ...);

Microsoft Specific —>

To maintain compatibility with previous versions of Microsoft C, a Microsoft extension to the ANSI C standard allows a comma without trailing periods (,) at the end of the list of parameters to indicate a variable number of arguments. However, it is recommended that code be changed to incorporate the ellipsis notation.

END Microsoft Specific

[解决办法]
可变参数,类似于printf的声明


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

[解决办法]
引用:
Quote: 引用:

...是变参数,也就是说如果使用这样的定义 参数的数目是可变的 而如果你使用你说的方法 那么如果希望传入多个参数的时候 就不行了
可变参数这样定义的话,后面的参数类型也要和第一个保持一致??


不需要 你可以看一下《C陷阱和缺陷》最后面对这方面的介绍还是不错的
[解决办法]
对学习编程者的忠告:
眼过千遍不如手过一遍!
书看千行不如手敲一行!
手敲千行不如单步一行!
单步源代码千行不如单步对应汇编一行!

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

热点排行