Problems with va_start, va_end, and so on

Hi again,

i'm having problems with functions without an exact number of parameters. This is my code:

byte *add_position(const char *fmt, ...) {
    va_list ap;
    int size, index;
    byte *posarray;
    
    size = get_fmt_size(fmt);
    posarray = (byte *)calloc(size, 1);
    
    va_start(ap, fmt);
    for(index=0; index<size; index++) {
        posarray[index] = va_arg(ap, byte);
    }
    va_end(ap);
    
    return posarray;
}

You can see that I use calloc and va_xxx family of functions. I really didn't know i could use them since i saw on Matrix.cpp shipped with arduino distribution. What are all the libraries i can use with arduino? Is it documented?

My problem is compiling that code, it gives the following message:

In function `byte* add_position(const char*, ...)':
note: if this code is reached, the program will abort

and "va_start(ap, fmt);" is highlighted.

What's the problem?

The va_... functions aren't really supported in Arduino - sometimes we do some magic behind the scenes, but I don't know we want to document it for standard use.

The va_... functions aren't really supported in Arduino - sometimes we do some magic behind the scenes, but I don't know we want to document it for standard use.

and what about using "calloc"?

anything avr-gcc can compile will work in arduino?

Anything avr-gcc can compile should work in Arduino, yes. The va_... error you got baffles me, because I would expect it to work. calloc() should work without any problems, but I'm not sure if I've tried it.

thanks for your quick responses, it's of great help to know it :slight_smile:

oops i got it, the problem goes out if i comment the for code.

va_start and va_end compile fine, something happens with the for.

edit:

ok, it's the va_arg function. without it all compile nicely.