如何在没有额外参数的情况下使用 C 中的 va_list 设置默认值

发布于 2024-12-27 03:46:20 字数 803 浏览 2 评论 0原文

当我尝试编写一个在没有给出额外参数的情况下具有默认值的函数时,我遇到了问题。我尝试检测给出的唯一参数是否等于 NULL(如其他答案中所建议的),但它似乎对我不起作用。

该函数的实际实现采用一个结构并将其添加到第二个参数中给出的链表中。如果没有给出第二个参数,我希望它将它添加到先前定义的默认全局链接列表中。

下面是使用 int 类型参数的更简单版本,但我想要做的原理是相同的:

/* appropriate headers... */

void test(int a, ... ) {

   int b;

   va_list args;
   va_start(args,a);

   b = va_arg(args,int);

   if (b == NULL) { // check if no argument is given and set default value
       b = 0; 
   } // if b != NULL then it should be set to the value of the argument
   va_end(args);

   printf("%d %d\n",a,b);
}

int main() {
   test(1);
   test(1,1);
   return 0;
}

但是,这给出了输出:

1 *random memory address*
1 1

我想要的输出应该有第一行,因为

1 0

如果我不能使用此方法,那么有人知道我如何实现我想要的吗?提前致谢。

I've had problem when trying write a function which has a default value when no extra arguments are given. I've tried detecting if the only argument given is equal to NULL (as suggested in other answers) but it doesn't seem to be working for me.

The actual implementation of this function takes a struct and adds it to a linked list given in the second argument. If no second argument is given, I want it to add it to a default global linked list which has been previously defined.

Below is a simpler version using int type arguments, but the principle of what I want to do is the same:

/* appropriate headers... */

void test(int a, ... ) {

   int b;

   va_list args;
   va_start(args,a);

   b = va_arg(args,int);

   if (b == NULL) { // check if no argument is given and set default value
       b = 0; 
   } // if b != NULL then it should be set to the value of the argument
   va_end(args);

   printf("%d %d\n",a,b);
}

int main() {
   test(1);
   test(1,1);
   return 0;
}

However, this gives the output:

1 *random memory address*
1 1

The output I want should have the first line as

1 0

If I can't use this method then does anyone have any ideas how I can achieve what I want? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

も星光 2025-01-03 03:46:20

仅使用 va_list 无法完成您想要的操作。

您可以使用宏和 __VA_ARGS__ 来强制某个参数作为“终止符”出现在参数列表的最后。即:

#define TEST(a, ...) Test(a, __VA_ARGS__, 0)

请注意,我使用的是 Visual C++。其他编译器的实现 __VA_ARGS__ 可能略有不同,因此您可能需要调整实现。

There is no way to do what you want with just va_list.

You can use macros and __VA_ARGS__ to force a certain argument to show up last in your argument list as a "terminator." i.e.:

#define TEST(a, ...) Test(a, __VA_ARGS__, 0)

Note that I'm using Visual C++. Other compilers might implement __VA_ARGS__ slightly differently so you may need to tweak the implementation.

花间憩 2025-01-03 03:46:20

接受变量参数的函数必须能够以某种方式告诉它何时到达变量参数的末尾。这可以通过解析来自固定参数的信息(例如,告诉您传递了多少个变量参数的参数,或者告诉您应该遵循哪些参数的格式字符串),或者可以通过显式哨兵值来实现,例如作为空指针,位于变量参数的末尾。

你似乎想要一个重大的奇迹;抱歉,只有微小的奇迹。

您可以这样设计您的界面:

int test1(int x, struct list *list) { ...code to handle adding x to arbitrary list... }

int test0(int x) { return test1(x, &global_struct); }

然后,当您想要使用默认列表时,您可以调用 test0() ;当您想要指定不同的列表时,您可以调用 test1()

请注意,test0() 非常简单,因此非常适合 C99 内联 函数定义。

如果您使用 C++,您可以提供一个带有默认参数或重载函数的函数(两个参数列表和实现,如上所述,但名称相同)。当然,与 C 相比,C++ 中更不赞成使用全局变量。

Your function accepting variable arguments has to be able to tell somehow when it has reached the end of the variable arguments. This can be by parsing information from the fixed arguments (e.g. an argument which tells you how many variable arguments were passed, or a format string which tells you what arguments are supposed to follow), or it can be by an explicit sentinel value, such as a null pointer, at the end of the variable arguments.

You seem to be wanting a major miracle; I'm sorry, only minor miracles are available.

You can design your interfaces like this:

int test1(int x, struct list *list) { ...code to handle adding x to arbitrary list... }

int test0(int x) { return test1(x, &global_struct); }

You then call test0() when you want to use the default list, and test1() when you want specify a different list.

Note that test0() is so simple that it is a good candidate for C99 inline function definition.

If you were using C++ you could provide a function with a default argument or an overloaded function (two argument lists and implementations, as above, but the same name). Of course, even more than in C, the use of global variables is deprecated in C++.

自找没趣 2025-01-03 03:46:20

函数必须有某种方法来知道它被赋予了多少个参数。你的函数没有办法,所以无法运行。您可以创建两个函数。您可以有一个单独的“参数数量”参数。您可以包含一些其他参数来间接告诉它有多少个参数(例如 printf 使用)。但你必须以某种方式去做。

A function has to have some way to know how many arguments it has been given. Your function has no way, so it can't work. You could create two functions. You could have a separate "number of arguments" parameter. You could include some other parameter that indirectly tells it how many parameters it has (like printf uses). But you have to do it somehow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文