0

I have a function that works with variable arguments and looks like this:

static int getIntValue(const int min,const int max,va_list *vl)
{
   int   listValue;

   listValue=va_arg(*vl,int);
   if (listValue<min) listValue=min;
   else if (listValue>max) listValue=max;
   return listValue;
}


unsigned long init_if_list(int *var,va_list vl)
{
   char *listTag;

   listTag=va_arg(vl,char*);
   if (!strcmp(listTag,INIT_SHOWUI)) initValues.uiFlags=getIntValue(INT_MIN,INT_MAX,&vl);

This code compiles well with Windows/VisualStudio 2012 and "older" GCC versions (like 4.7 on CentOS 6). But it fails when I try to compile it with GCC 4.8.4 / Ubuntu. Here I get following error:

error: cannot convert '__va_list_tag**' to '__va_list_tag (*)[1]' for argument '3' to 'int getIntValue(int, int, __va_list_tag (*)[1])'
   if (!strcmp(listTag,INIT_SHOWUI))                initValues.uiFlags=getIntValue(INT_MIN,INT_MAX,&vl);

Anybody an idea what is wrong here? Why does GCC complain?

Thanks!

4
  • It's 2016. Why the varargs? Commented Apr 17, 2016 at 15:29
  • 1
    Don't pass the va_list argument as a pointer? If you look closer at the error message you will see that it already is a pointer, i.e. it's type-alias (typedef) for __va_list_tag*, so you don't need to use pointers to emulate pass-by-reference. Commented Apr 17, 2016 at 15:32
  • Lightness Races in Orbit: because DLL-interfaces still need to be plain C, C++ interfaces depend on exact version of compiler. Commented Apr 17, 2016 at 15:33
  • 1
    There are lots of things wrong with the way you use the va_list... Commented Apr 17, 2016 at 15:34

1 Answer 1

1

Always pass va_list by value, never by pointer.

Therefore:

static int getIntValue(const int min, const int max, va_list vl)

and

initValues.uiFlags=getIntValue(INT_MIN, INT_MAX, vl);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.