variadic

passing variable number of arguments

跟風遠走 提交于 2019-11-26 23:36:33
问题 Can we pass variable number of arguments to a function in c? 回答1: Here is an example: #include <stdlib.h> #include <stdarg.h> #include <stdio.h> int maxof(int, ...) ; void f(void); int main(void){ f(); exit(EXIT SUCCESS); } int maxof(int n_args, ...){ register int i; int max, a; va_list ap; va_start(ap, n_args); max = va_arg(ap, int); for(i = 2; i <= n_args; i++) { if((a = va_arg(ap, int)) > max) max = a; } va_end(ap); return max; } void f(void) { int i = 5; int j[256]; j[42] = 24; printf("%d

C++11 type trait to differentiate between enum class and regular enum

十年热恋 提交于 2019-11-26 16:50:01
问题 I'm writing a promotion template alias similar to boost::promote but for C++11. The purpose of this is to avoid warnings when retrieving arguments from varidic functions. e.g. template <typename T> std::vector<T> MakeArgVectorV(int aArgCount, va_list aArgList) { std::vector<T> args; while (aArgCount > 0) { args.push_back(static_cast<T>(va_arg(aArgList, Promote<T>))); --aArgCount; } return args; } The Promote template alias promotes the type following the default argument promotion for

Macro to count number of arguments

妖精的绣舞 提交于 2019-11-26 16:21:25
问题 I have a variadic function from a third-party C library: int func(int argc, ...); argc indicates the number of passed optional arguments. I'm wrapping it with a macro that counts the number of arguments, as suggested here. For reading convenience, here's the macro: #define PP_ARG_N( \ _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \ _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \ _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \ _41, _42,

Passing parameters dynamically to variadic functions

旧巷老猫 提交于 2019-11-26 11:31:55
问题 I was wondering if there was any way to pass parameters dynamically to variadic functions. i.e. If I have a function int some_function (int a, int b, ...){/*blah*/} and I am accepting a bunch of values from the user, I want some way of passing those values into the function: some_function (a,b, val1,val2,...,valn) I don\'t want to write different versions of all these functions, but I suspect there is no other option? 回答1: Variadic functions use a calling convention where the caller is

Objective-C passing around … nil terminated argument lists

孤街浪徒 提交于 2019-11-26 10:56:08
问题 Having some issues with the ... in ObjectiveC. I\'m basically wrapping a method and want to accept a nil terminated list and directly pass that same list to the method I am wrapping. Here\'s what I have but it causes an EXC_BAD_ACCESS crash. Inspecting the local vars, it appears when otherButtonTitles is simply a NSString when it is passed in with otherButtonTitles:@\"Foo\", nil] + (void)showWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:

Forward an invocation of a variadic function in C

拈花ヽ惹草 提交于 2019-11-26 01:27:21
问题 In C, is it possible to forward the invocation of a variadic function? As in, int my_printf(char *fmt, ...) { fprintf(stderr, \"Calling printf with fmt %s\", fmt); return SOMEHOW_INVOKE_LIBC_PRINTF; } Forwarding the invocation in the manner above obviously isn\'t strictly necessary in this case (since you could log invocations in other ways, or use vfprintf), but the codebase I\'m working on requires the wrapper to do some actual work, and doesn\'t have (and can\'t have added) a helper

Why use the params keyword?

大兔子大兔子 提交于 2019-11-26 00:45:33
问题 I know this is a basic question, but I couldn\'t find an answer. Why use it? if you write a function or a method that\'s using it, when you remove it the code will still work perfectly, 100% as without it. E.g: With params: static public int addTwoEach(params int[] args) { int sum = 0; foreach (var item in args) sum += item + 2; return sum; } Without params: static public int addTwoEach(int[] args) { int sum = 0; foreach (var item in args) sum += item + 2; return sum; } 回答1: With params you

Is it possible to iterate over arguments in variadic macros?

我的梦境 提交于 2019-11-26 00:32:46
问题 I was wondering if it is possible to iterate over arguments passed to a variadic macro in C99 or using any GCC extensions ? For e.g. is it possible to write a generic macro that takes a structure and its fields passed as arguments and prints offset of each field within the structure ? Something like this: struct a { int a; int b; int c; }; /* PRN_STRUCT_OFFSETS will print offset of each of the fields within structure passed as the first argument. */ int main(int argc, char *argv[]) { PRN

How to make a variadic macro (variable number of arguments)

人走茶凉 提交于 2019-11-26 00:17:55
问题 I want to write a macro in C that accepts any number of parameters, not a specific number example: #define macro( X ) something_complicated( whatever( X ) ) where X is any number of parameters I need this because whatever is overloaded and can be called with 2 or 4 parameters. I tried defining the macro twice, but the second definition overwrote the first one! The compiler I\'m working with is g++ (more specifically, mingw) 回答1: C99 way, also supported by VC++ compiler. #define FOO(fmt, ...)

How to use R&#39;s ellipsis feature when writing your own function?

被刻印的时光 ゝ 提交于 2019-11-25 23:09:08
问题 The R language has a nifty feature for defining functions that can take a variable number of arguments. For example, the function data.frame takes any number of arguments, and each argument becomes the data for a column in the resulting data table. Example usage: > data.frame(letters=c(\"a\", \"b\", \"c\"), numbers=c(1,2,3), notes=c(\"do\", \"re\", \"mi\")) letters numbers notes 1 a 1 do 2 b 2 re 3 c 3 mi The function\'s signature includes an ellipsis, like this: function (..., row.names =