T
thierrydollar
Hi,
I have written a very simple program using variable arguments calls and
I
get strange things that I cannot explain.
I have one function (add) that displays two parameters. It works well
when I call it directly.
Now I have a second function (set) that also calls add. But this
doesn't
work.
Here is the output generated by my program. I don't understand why I
get different result (I tried on both Borland C++ Builder and in Visual
C++, and
I got similar results).
Any help would be greatly appreciated.
Thanks
Best Regards
Thierry
format = H%dW%d
arg[] = 1234
arg[] = 5678
format = H%dW%d
arg[] = 1245060
arg[] = 1245060
#include <stdio.h>
#include <stdarg.h>
//---------------------------------------------------------------------------
void add (char *format, ...)
{
va_list arg_ptr;
va_start (arg_ptr, format);
printf("format = %s\n",format);
printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
}
//---------------------------------------------------------------------------
void set (char *format, ...)
{
va_list arg_ptr;
va_start (arg_ptr, format);
add(format, arg_ptr);
}
//---------------------------------------------------------------------------
int main()
{
printf("\n>>>ADD \n");
add("H%dW%d",1234,5678);
printf("\n>>>SET \n");
set("H%dW%d",1234,5678);
return 0;
}
I have written a very simple program using variable arguments calls and
I
get strange things that I cannot explain.
I have one function (add) that displays two parameters. It works well
when I call it directly.
Now I have a second function (set) that also calls add. But this
doesn't
work.
Here is the output generated by my program. I don't understand why I
get different result (I tried on both Borland C++ Builder and in Visual
C++, and
I got similar results).
Any help would be greatly appreciated.
Thanks
Best Regards
Thierry
format = H%dW%d
arg[] = 1234
arg[] = 5678
format = H%dW%d
arg[] = 1245060
arg[] = 1245060
#include <stdio.h>
#include <stdarg.h>
//---------------------------------------------------------------------------
void add (char *format, ...)
{
va_list arg_ptr;
va_start (arg_ptr, format);
printf("format = %s\n",format);
printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
}
//---------------------------------------------------------------------------
void set (char *format, ...)
{
va_list arg_ptr;
va_start (arg_ptr, format);
add(format, arg_ptr);
}
//---------------------------------------------------------------------------
int main()
{
printf("\n>>>ADD \n");
add("H%dW%d",1234,5678);
printf("\n>>>SET \n");
set("H%dW%d",1234,5678);
return 0;
}