variadic-functions

Passing lists from one function to another in Swift

大兔子大兔子 提交于 2019-11-26 08:24:10
问题 I can\'t quite understand why I can\'t past an Int[] from one function to another: func sumOf(numbers: Int...) -> Int { var sum = 0 for number in numbers { sum += number } return sum } func average(numbers:Int...) -> Double { var sum = sumOf(numbers) return Double(sum) / Double(numbers.count) } This gives me the following error: Playground execution failed: error: <REPL>:138:19: error: could not find an overload for \'__conversion\' that accepts the supplied arguments var sum = sumOf(numbers)

How are variable arguments implemented in gcc?

橙三吉。 提交于 2019-11-26 08:16:20
问题 int max(int n, ...) I am using cdecl calling convention where the caller cleans up the variable after the callee returns. I am interested in knowing how do the macros va_end , va_start and va_arg work? Does the caller pass in the address of the array of arguments as the second argument to max? 回答1: If you look at the way the C language stores the parameters on the stack, the way the macros work should become clear:- Higher memory address Last parameter Penultimate parameter .... Second

Python, default keyword arguments after variable length positional arguments

梦想的初衷 提交于 2019-11-26 08:15:08
问题 I thought I could use named parameters after variable-length positional parameters in a function call in Python 2, but I get a SyntaxError when importing a python class. I\'m writing with the following \"get\" method, for example: class Foo(object): def __init__(self): print \"You have created a Foo.\" def get(self, *args, raw=False, vars=None): print len(args) print raw print vars The error looks like: def get(self, *args, raw=False, vars=None): ^ SyntaxError: invalid syntax I\'d like to be

Passing variable arguments to another function that accepts a variable argument list

人走茶凉 提交于 2019-11-26 08:07:48
So I have 2 functions that both have similar arguments void example(int a, int b, ...); void exampleB(int b, ...); Now example calls exampleB , but how can I pass along the variables in the variable argument list without modifying exampleB (as this is already used elsewhere too). You can't do it directly; you have to create a function that takes a va_list : #include <stdarg.h> static void exampleV(int b, va_list args); void example(int a, int b, ...) { va_list args; va_start(args, b); exampleV(b, args); va_end(args); } void exampleB(int b, ...) { va_list args; va_start(args, b); exampleV(b,

How to create variable argument methods in Objective-C

拈花ヽ惹草 提交于 2019-11-26 07:59:54
问题 Maybe this will be obviously simple for most of you, but could you please give an example how to create similar methods (in Objective-C) and functions in C to create functions like NSString \'s stringWithFormat: , or NSLog() . Just to remind: [NSString stringWithFormat:@\"example tekst %i %@ %.2f\", 122, @\"sth\", 3.1415\"]; NSLog(@\"account ID %i email %@\", accountID, email); I\'d like to create the similar to NSString \'s method stringWithFormat: , NSURL - urlWithFormat . 回答1: What these

How can you pass multiple primitive parameters to AsyncTask?

风格不统一 提交于 2019-11-26 06:57:08
问题 There are related questions, such as How can I pass in 2 parameters to a AsyncTask class? , but I ran into the difficulty of trying in vain to pass multiple primitives as parameters to an AsyncTask, so I want to share what I discovered. This subtlety is not captured in the existing questions and answers, so I want to help out anyone who runs into the same problem as I did and save them the pain. The question is this: I have multiple primitive parameters (e.g. two longs) that I want to pass to

How to work with varargs and reflection

岁酱吖の 提交于 2019-11-26 06:39:39
问题 Simple question, how make this code working ? public class T { public static void main(String[] args) throws Exception { new T().m(); } public // as mentioned by Bozho void foo(String... s) { System.err.println(s[0]); } void m() throws Exception { String[] a = new String[]{\"hello\", \"kitty\"}; System.err.println(a.getClass()); Method m = getClass().getMethod(\"foo\", a.getClass()); m.invoke(this, (Object[]) a); } } Output: class [Ljava.lang.String; Exception in thread \"main\" java.lang

What is the format of the x86_64 va_list structure?

a 夏天 提交于 2019-11-26 06:37:25
问题 Anyone have a reference for the representation of va_list in the x86_64 ABI (the one used on Linux)? I\'m trying to debug some code where the stack or arguments seem corrupt and it would really help to understand what I\'m supposed to be seeing... 回答1: I made my comment into an answer. This may help. It's a reference, albeit lightweight ( EDIT : original link dead; replaced Wayback Machine-preserved link). The Variable Argument List reference starts on page 50, then it goes on, page 52-53

What is the purpose of the h and hh modifiers for printf?

本秂侑毒 提交于 2019-11-26 06:06:15
问题 Aside from %hn and %hhn (where the h or hh specifies the size of the pointed-to object), what is the point of the h and hh modifiers for printf format specifiers? Due to default promotions which are required by the standard to be applied for variadic functions, it is impossible to pass arguments of type char or short (or any signed/unsigned variants thereof) to printf . According to 7.19.6.1(7), the h modifier: Specifies that a following d, i, o, u, x, or X conversion specifier applies to a

call printf using va_list

做~自己de王妃 提交于 2019-11-26 05:29:42
问题 void TestPrint(char* format, ...) { va_list argList; va_start(argList, format); printf(format, argList); va_end(argList); } int main() { TestPrint(\"Test print %s %d\\n\", \"string\", 55); return 0; } I need to get: Test print string 55 Actually, I get garbage output. What is wrong in this code? 回答1: Use vprintf() instead. 回答2: Instead of printf, I recommend you try vprintf instead, which was created for this specific purpose: #include <stdio.h> #include <stdlib.h> #include <stdarg.h> void