variadic-functions

call printf using va_list

拈花ヽ惹草 提交于 2019-11-26 17:36:49
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? Use vprintf() instead. 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 errmsg( const char* format, ... ) { va_list arglist; printf( "Error: " ); va_start( arglist, format ); vprintf( format,

Varargs Java Ambiguous Call

雨燕双飞 提交于 2019-11-26 17:32:08
问题 I'm a little confused about Java's varargs methods: public static int sum(int ...a) { return 0; } public static double sum(double ...a) { return 0.0; } When I tried to invoke sum() without passing any argument, then the int version of method was invoked. I don't understand why; normally the compiler must raise an error. By contrast, the following piece of code generates a compiler error when I try to invoke sum without any argument: public static int sum(int ...a) { return 0; } public static

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

牧云@^-^@ 提交于 2019-11-26 17:24:48
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 short int or unsigned short int argument (the argument will have been promoted according to the integer

Variadic function (va_arg) doesn't work with float?

本秂侑毒 提交于 2019-11-26 16:50:54
问题 I have a variadic function which takes a float parameter. Why doesn't it work? va_arg(arg, float) 回答1: Parameters of functions that correspond to ... are promoted before passing to your variadic function. char and short are promoted to int , float is promoted to double , etc. 6.5.2.2.7 The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments. The

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

fake va_list in ARC

走远了吗. 提交于 2019-11-26 16:34:35
问题 I need to create in an iOS application a fake va_list to pass to a NSString initWithFormat:arguments: function, this is my code: NSArray *fixedArguments = [[NSArray alloc] initWithArray:arguments]; NSRange range = NSMakeRange(0, [fixedArguments count]); va_list fakeArgList = (va_list)malloc(sizeof(NSString *) * [fixedArguments count]); __unsafe_unretained id *ptr = (__unsafe_unretained id *)fakeArgList; [fixedArguments getObjects:ptr range:range]; content = [[NSString alloc] initWithFormat

Does printf(“%x”,1) invoke undefined behavior?

孤街醉人 提交于 2019-11-26 16:18:41
According to the C standard (6.5.2.2 paragraph 6) If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the behavior is undefined. If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the

How to count the number of arguments passed to a function that accepts a variable number of arguments?

a 夏天 提交于 2019-11-26 16:04:35
问题 How to count the no of arguments passed to the function in following program: #include<stdio.h> #include<stdarg.h> void varfun(int i, ...); int main(){ varfun(1, 2, 3, 4, 5, 6); return 0; } void varfun(int n_args, ...){ va_list ap; int i, t; va_start(ap, n_args); for(i=0;t = va_arg(ap, int);i++){ printf("%d", t); } va_end(ap); } This program's output over my gcc compiler under ubuntu 10.04: 234561345138032514932134513792 so how to find how many no. of arguments actually passed to the function

How to reverse the order of arguments of a variadic template function?

断了今生、忘了曾经 提交于 2019-11-26 15:53:15
问题 I have a template function with varargs template arguments , like this template<typename Args...> void ascendingPrint(Args... args) { /* ... */ } And I want to write template<typename Args...> void descendingPrint(Args... args) { /* implementation using ascendingPrint()? */ } How do I reverse the order of the parameter-pack args before passing it along, i.e. in pseudo-code: template<typename Args...> void descendingPrint(Args... args) { ascendingPrint( reverse(args) ); } 回答1: Here is a

How to pass an ArrayList to a varargs method parameter?

这一生的挚爱 提交于 2019-11-26 15:53:00
Basically I have an ArrayList of locations: ArrayList<WorldLocation> locations = new ArrayList<WorldLocation>(); below this I call the following method: .getMap(); the parameters in the getMap() method are: getMap(WorldLocation... locations) The problem I'm having is I'm not sure how to pass in the WHOLE list of locations into that method. I've tried .getMap(locations.toArray()) but getMap doesn't accept that because it doesn't accept Objects[]. Now if I use .getMap(locations.get(0)); it will work perfectly... but I need to somehow pass in ALL of the locations... I could of course make keep