1

I have myPrintf() with variadic parameters. As I use it directly then everythink work fine. But as I try to call it from another variadic function then i got odd values. What I do wrong? I also need to call both funtions as below

myPrintf("%i %+.0f %+.0f %+.0f %+.0f %+.0f %+.0f %+.0f\n",millis(),ax,ay,az,temp,gx,gy,gz); // working
LOGSD("%i %+.0f %+.0f %+.0f %+.0f %+.0f %+.0f %+.0f\n",millis(),ax,ay,az,temp,gx,gy,gz); // not working


void LOGSD(const char *format, ...){
    va_list arg;
    va_start(arg, format);
    myPrintf(format,arg);
    va_end(arg);
    return;
}

void myPrintf(const char *format, ...){
 // some code here
}


example from calling myPrintf():
413137 -1112 -420 +15952 +3726 -15 +201 +90
413237 -1168 -364 +15924 +3730 -17 +206 +95
413337 -1044 -452 +15932 +3722 -37 +200 +97
413437 -1152 -360 +15876 +3732 -20 +213 +99
413537 -1120 -352 +15884 +3706 -28 +225 +96
413637 -1136 -336 +15864 +3733 -35 +214 +109
413737 -1092 -372 +15968 +3725 -35 +212 +73
413838 -1052 -340 +15808 +3721 -40 +247 +103

example from calling LOGSD():
1073451856 +0 +0 -1060 -328 +0 +2 +0
1073451856 +0 +0 -1064 -364 +2 +2 +0
1073451856 +0 +0 -1056 -400 +2 +2 +0
1073451856 +0 +0 -1132 -408 +2 +2 +0
1073451856 +0 +0 -1008 -372 +2 +2 +0
1073451856 +0 +0 -1096 -376 +2 +2 +0
1073451856 +0 +0 -1132 -296 +0 +2 +0
1073451856 +0 +0 -1148 -372 +0 +2 +0
Dobijasek
  • 91
  • 1
  • 5
  • You can't just pass `va_list` to a function that is not specifically defined to expect one. It doesn't magically expand a variadic parameter list. A common approach is to write a function that takes `va_list` as a parameter, and walks it using `va_arg`; and another function with variadic parameters, that creates `va_list` and passes it to the first function. See e.g. `printf` and `vprintf` - in a typical implementation, the former is a simple wrapper for the latter, the latter does all the work. – Igor Tandetnik Mar 14 '21 at 15:07
  • So, write `myVPrintf` that takes the format and `va_list` argument, and does the real work; and call it from both `myPrintf` and `LOGSD` – Igor Tandetnik Mar 14 '21 at 15:09
  • You could simply use a template to pass the parameters on: `template void LOGSD(const char* format, Ts ... args) { myPrintf(format, args...); }` – fabian Mar 14 '21 at 15:11

0 Answers0