简体   繁体   English

为什么va_arg在char *类型的变量参数列表的末尾返回NULL?

[英]Why is va_arg returning NULL at the end of a variable argument list of type char*?

This is what has been said about va_arg in the reputed link below: 这就是以下知名链接中关于va_arg说法:

http://www.cplusplus.com/reference/cstdarg/va_arg/ http://www.cplusplus.com/reference/cstdarg/va_arg/

Notice also that va_arg does not determine either whether the retrieved argument is the last argument passed to the function (or even if it is an element past the end of that list). The function should be designed in such a way that the number of parameters can be inferred in some way by the values of either the named parameters or the additional arguments already read.

In addition to that, in the so-so book from which I read about va_arg ,all examples have made sure that one of the fixed arguments is always the number/count of variable arguments we would be passing.And this count is used in a loop that advances va_arg to the next item,and the loop condition (using the count) makes sure that it exits when va_arg retrieves the last argument in the variable list.This seems to corroborate the above paragraph from the site which says that "the function should be designed in such a way........" (above) . 除此之外,在我从中读到关于va_arg本书中,所有示例都确保其中一个fixed参数始终是我们将要传递的变量参数的数量/数量。此计数用于将va_arg推进到下一个项目的循环,并且循环条件(使用计数)确保当va_arg检索变量列表中的最后一个参数时它退出。这似乎证实了上述段落中的"the function should be designed in such a way........" (above)

So in blunt words, va_arg is kinda dumb.But in the following example taken from that website for va_end , va_arg suddenly seems to act smartly.When the end of a variable argument list of type char* is reached, it returns a NULL pointer. 所以直言不讳地说, va_arg有点愚蠢。但是在下面的例子中,从va_end网站上看, va_arg似乎突然表现得很聪明。当到达char*类型的变量参数列表的末尾时,它返回一个NULL指针。 How is it so? 怎么回事? The topmost paragraph I linked clearly states 我联系的最重要段落明确指出

"va_arg does not determine either whether the retrieved argument is the last argument passed to the function (or even if it is an element past the end of that list"

and further,there is nothing in the following program that ensures that a NULL pointer should be returned when the end of the variable argument list is crossed.So how come va_arg returns a NULL pointer here at the end of the list? 并且,以下程序中没有任何内容确保在交叉变量参数列表的末尾时应返回NULL指针。那么为什么va_arg在列表的末尾返回NULL指针?

/* va_end example */
#include <stdio.h>      /* puts */
#include <stdarg.h>     /* va_list, va_start, va_arg, va_end */

void PrintLines (char* first, ...)
{
  char* str;
  va_list vl;

  str=first;

  va_start(vl,first);

  do {
    puts(str);
    str=va_arg(vl,char*);
  } while (str!=NULL);

  va_end(vl);
}

int main ()
{
  PrintLines ("First","Second","Third","Fourth",NULL);
  return 0;
}    

Output 产量

First

Second

Third

Fourth

Program Source Link 节目源链接

When the end of a variable argument list of type char * is reached, it returns a NULL pointer. 当到达char *类型的变量参数列表的末尾时,它返回NULL指针。 How is it so? 怎么回事?

Because the last argument passed to the function is indeed NULL : 因为传递给函数的最后一个参数确实是NULL

PrintLines("First", "Second", "Third", "Fourth", NULL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM