English 中文(简体)
如果在C区没有使用“va_list”号的附加论点,如何设定违约价值
原标题:How to set a default value when no extra arguments are present using va_list in C

我在试图写写一份在不提出额外论点时具有违约价值的职务时遇到问题。 我试图发现,如果提出的唯一论点(如其他答复所建议的)等同于全国人民力量联盟,但似乎并没有为我工作。

实际履行这一职能具有阻碍作用,并增加了第二点提出的相关清单。 如果没有提出第二个论点,我想在先前界定的缺省全球联系清单中添加这一论点。

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
",a,b);
}

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

然而,这一产出是:

1 *random memory address*
1 1

我所希望的产出应该有第一行。

1 0

如果我能不使用这种方法,那么任何人都对我能如何实现我所想要的东西有任何想法? 提前感谢。

最佳回答

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

您可使用宏观和<代码>VA_ARGS__强迫某一论点在您的论据清单上作为“决定因素”加以最后陈述。 i.e:

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

注:Im使用视像C 0。 其他汇编者可实施“指南>-VA_ARGS_ ,但以略微不同的方式加以实施,因此,您可能需要对执行工作进行分类。

问题回答

你接受可变论点的职能,在达到可变论点的终结时,必须能够讲几句。 可以通过将信息从固定论点中加以区分(例如,一个论点告诉你,通过多少可变的论据,或一种形式,向你们说明应该遵循哪些论点),或者在可变论点的末尾,可以采用明确的派任价值,如一个无效点。

你们似乎想要一个大小小小小小小小小小小小小小小小小小小.。

您可以设计这样的接口:

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() 如此简单,成为C99inline功能定义的良好候选人。

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++.

一项职能必须在一定程度上了解它提出多少论点。 你们的职能是毫无结果的,因此它无法发挥作用。 你可以设立两个职能。 你可以有单独的“论点数目”参数。 你们可以包括一些间接告诉它有多少参数的其他参数(如<条码>f使用)。 但你们必须做些什么。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签