如何将数组作为参数传递给可变参数函数? [英] How can I pass an array as parameters to a vararg function?
问题描述
我有一些看起来像这样的代码:
I have some code that looks like this:
uint8_t activities[8];
uint8_t numActivities = 0;
...
activities[numActivities++] = someValue;
...
activities[numActivities++] = someOtherValue;
...
switch (numActivities)
{
0 : break;
1 : LogEvent(1, activities[0]); break;
2 : LogEvent(1, activities[0], activities[1]); break;
3 : LogEvent(1, activities[0], activities[1], activities[2]); break;
// and so on
}
其中 LogEvent() 是可变参数函数.
where LogEvent() is a varargs function.
有没有更优雅的方式来做到这一点?
Is there a more elgant way to do this?
[更新] 向@0x69 等人致歉.我省略了说,LogEvent() 不能将数组作为参数的情况很多.对不起.
[Update] Aplogies to @0x69 et al. I omitted to say that there are many cases where LogEvent() could not take an array as a parameter. Sorry.
推荐答案
没有标准的方法来构造或操作 va_args
参数,甚至将它们传递给另一个函数 (操作可变参数的标准方法?, C 编程:前向变量参数列表).您最好看看是否可以访问 LogEvent
的内部例程.
There's no standard way to construct or manipulate va_args
arguments, or even pass them to another function (Standard way to manipulate variadic arguments?, C Programming: Forward variable argument list). You'd be better off seeing if you can access the internal routines of LogEvent
.
这篇关于如何将数组作为参数传递给可变参数函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!