Tag: variadic functions

使用带有任意数量参数的函数的参数,单位为C.

我刚才读到: “C void arguments”关于C中这些函数定义之间的差异: int f(void) 和 int f() 理解第二种forms意味着函数返回带有任意数量参数的整数,我想知道我们如何才能实际访问和使用这些未知参数 ? 我很想得到示例代码和解释。 另外,我知道C语言中的Varargs的机制(使用va_arg , va_end , va_start函数),并且很高兴听到这种机制与上面提到的f()forms之间的差异 。 非常感谢!

如何从c / c ++中的跟随函数获取所有参数?

以下是我的方法的实现 static VALUE myMethod(VALUE self, VALUE exc, const char* fmt, …) { // Need to get all the arguments passed to this function and print it } 函数调用如下: myMethod(exception, “”Exception message: %s, Exception object %d”, “Hi from Exception”, 100); 你能为myMethod()提供代码来访问所有参数并打印出来。 提前致谢。

无法使用变量参数实现函数

我试图用变量参数实现函数,但是将垃圾值作为输出。我在尝试自己实现之前已经参考过这篇文章 。可以任何人帮我解决这段代码,因为我无法理解这段代码中的错误。 /* va_arg example */ #include /* printf */ int FindMax (int n, …) { int i,val,largest,*p; p=&n; p+=sizeof(int); largest=*p; for (i=1;ival)?largest:val; } return largest; } int main () { int m; m= FindMax (7,702,422,631,834,892,104,772); printf (“The largest value is: %d\n”,m); return 0; }

内联vararg函数

在玩优化设置时,我注意到一个有趣的现象:采用可变数量参数( … )的函数似乎从未被内联。 (显然这种行为是特定于编译器的,但我已经在几个不同的系统上进行了测试。) 例如,编译以下小程序: #include #include static inline void test(const char *format, …) { va_list ap; va_start(ap, format); vprintf(format, ap); va_end(ap); } int main() { test(“Hello %s\n”, “world”); return 0; } 似乎总会导致出现在可执行的可执行文件中的(可能是损坏的) test符号(在MacOS和Linux上以C和C ++模式使用Clang和GCC进行测试)。 如果修改了test()的签名以获取传递给printf()的普通字符串,那么两个编译器都会从-O1向上内联函数,如您所期望的那样。 我怀疑这与用于实现varargs的伏都教魔法有关,但通常这样做对我来说是个谜。 任何人都可以告诉我编译器通常如何实现vararg函数,以及为什么这似乎阻止了内联?

存储va_list以便以后在C / C ++中使用的最佳方法

我正在使用va_list来构造一个呈现的字符串。 void Text2D::SetText(const char *szText, …) 这一切都很好,但现在用户可以在应用程序运行时更改语言。 我需要重新生成所有文本字符串并在初始化后重新缓存文本位图。 我想存储va_list并在需要生成文本时使用它。 为了给你一些更多的背景知识,这需要在我正在翻译的关键字符串中包含动态数据的情况下发生。 “Player Score:%d” 这是我需要翻译的关键字符串。 我希望保留va_list中提供的数字以供以后使用(在初始化文本的函数范围之外),以便在初始化后需要重新翻译。 我希望保留一份va_list的副本,以便与vsnprintf一起使用。 我已经做了一些研究,并找到了一些方法。 其中一些我质疑它是否是一种合适的方法(在稳定和便携方面)。

C ++中函数的可变参数数量

我如何在C ++中的函数中拥有可变数量的参数。 C#中的模拟: public void Foo(params int[] a) { for (int i = 0; i < a.Length; i++) Console.WriteLine(a[i]); } public void UseFoo() { Foo(); Foo(1); Foo(1, 2); } Java模拟: public void Foo(int… a) { for (int i = 0; i < a.length; i++) System.out.println(a[i]); } public void UseFoo() { Foo(); Foo(1); Foo(2); }

如何在Win64上使用Varargs和C中的函数指针一起使用?

考虑以下C程序: #include #include typedef void (callptr)(); static void fixed(void *something, double val) { printf(“%f\n”, val); } static void dynamic(void *something, …) { va_list args; va_start(args, something); double arg = va_arg(args, double); printf(“%f\n”, arg); } int main() { double x = 1337.1337; callptr *dynamic_func = (callptr *) &dynamic; dynamic_func(NULL, x); callptr *fixed_func = (callptr *) &fixed; […]

可变参数函数参数的自动类型促销是什么?

请考虑以下代码段: #include #include void display(int num, …) { char c; int j; va_list ptr; va_start(ptr,num); for (j= 1; j <= num; j++){ c = va_arg(ptr, char); printf("%c", c); } va_end(ptr); } int main() { display(4, 'A', 'a', 'b', 'c'); return 0; } 该程序给出了运行时错误,因为vararg自动将char提升为int,在这种情况下我应该使用int。 当我使用vararg时,允许使用哪些类型,如何知道使用哪种类型以及避免此类运行时错误。

如何在gcc中实现变量参数?

int max(int n, …) 我正在使用cdecl调用约定,其中调用者在被调用者返回后清理变量。 我有兴趣知道宏va_end , va_start和va_arg工作的? 调用者是否将参数数组的地址作为max的第二个参数传递?