【发布时间】:2016-08-22 07:06:28
【问题描述】:
如何在c中组合字节数组以接受可变数量的参数(可变参数函数)?
typedef struct {
unsigned char *data;
int length;
} bytes;
// Problem in here how to combine byte arrays
// for accepts a variable number of arguments
bytes bytesConcat(bytes fmt, ...) {
return b1 + b2 + ...b(n)
}
static unsigned char src1[] = { 1,2 };
static unsigned char src2[] = { 3,4 };
void main() {
bytes b1, b2;
b1.data = src1;
b1.length = sizeof(src1);
b2.data = src2;
b2.length = sizeof(src2);
// call byteConcat with passing two arguments
// result1 expected is 1,2,3,4
bytes result1 = bytesConcat(b1, b2);
}
提前致谢
【问题讨论】:
-
这不仅仅是可变数量的参数;我认为您必须考虑的主要问题是内存管理。处理各种参数很容易:也请查看this question。
-
bytesConcat函数中如何从fmt获取参数列表?
-
@anto 通过使用
stdarg.h库。当您在使用它时遇到特定问题时,请告诉我们。 -
@2501,感谢您的指出。
标签: c arrays concat variadic-functions