www.digitalmars.com         C & C++   DMDScript  

D - Bug with variable number of arguments?

reply Marcel Strik <mars_888 hotmail.com> writes:
When I try this piece of code the outcome seems (to me at least) not as 
it should be. Is this is a bug or am I just missing something in the way 
parameters are passed?
(Print function based on code from Sean L. Palmer as posted in the 
'va_list / rolling your own printf / Outbuffer' thread. I couldn't find 
any other information on it so there might just be something else which 
would explain for the behaviour).


import c.stdio;
import string;

void Print( char[] msg, ... )
{
	va_list ap;
	ap = cast(va_list)&msg;
	ap += msg.size;
	
	char[] buf;
	buf.length = 1024;
	vsprintf(buf, msg, ap);
	buf.length = strlen(buf);

	printf( "%.*s", buf );
}

int main( char [] [] args ) 
{
	float test = 1.2345;
	Print( "%f\n", 1.2345 );
	Print( "%f\n", test );

	return 1;
}

This outputs:

1.234500
0.000000

- Marcel
Oct 25 2003
parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
Try changing test from float to double, or put an 'f' on the end of your
1.2345.  Should make clear what's going on.

I don't think D promotes va_list arguments automatically the same way C
does.

Also I think it's lame how you're having to typecast to va_list and skip the
msg buffer manually.  There has to be a better way.  We should be able to
name the '...' parameter and refer to it directly as a va_list.

Sean

"Marcel Strik" <mars_888 hotmail.com> wrote in message
news:Xns941FE6B8F27D5marsvulcanushotmail 63.105.9.61...
 When I try this piece of code the outcome seems (to me at least) not as
 it should be. Is this is a bug or am I just missing something in the way
 parameters are passed?
 (Print function based on code from Sean L. Palmer as posted in the
 'va_list / rolling your own printf / Outbuffer' thread. I couldn't find
 any other information on it so there might just be something else which
 would explain for the behaviour).


 import c.stdio;
 import string;

 void Print( char[] msg, ... )
 {
 va_list ap;
 ap = cast(va_list)&msg;
 ap += msg.size;

 char[] buf;
 buf.length = 1024;
 vsprintf(buf, msg, ap);
 buf.length = strlen(buf);

 printf( "%.*s", buf );
 }

 int main( char [] [] args )
 {
 float test = 1.2345;
 Print( "%f\n", 1.2345 );
 Print( "%f\n", test );

 return 1;
 }

 This outputs:

 1.234500
 0.000000

 - Marcel

Oct 25 2003
parent Marcel <mars_888 hotmail.com> writes:
Thanks for the quick reply and solution. Casting the values to doubles 
was about the only thing I hadn't tried yet. I guess this type of 
behaviour is just an artifact of interfacing D with a C function.

It would indeed be nice if there was better support for multiple 
arguments in D or at least a better way to do the same (The C/C++ way 
seems a bit outdated to me).

- Marcel

"Sean L. Palmer" <palmer.sean verizon.net> wrote in
news:bnen7u$1g2m$1 digitaldaemon.com: 

 Try changing test from float to double, or put an 'f' on the end of
 your 1.2345.  Should make clear what's going on.
 
 I don't think D promotes va_list arguments automatically the same way
 C does.
 
 Also I think it's lame how you're having to typecast to va_list and
 skip the msg buffer manually.  There has to be a better way.  We
 should be able to name the '...' parameter and refer to it directly as
 a va_list. 
 
 Sean
 
 "Marcel Strik" <mars_888 hotmail.com> wrote in message
 news:Xns941FE6B8F27D5marsvulcanushotmail 63.105.9.61...
 When I try this piece of code the outcome seems (to me at least) not
 as it should be. Is this is a bug or am I just missing something in
 the way parameters are passed?
 (Print function based on code from Sean L. Palmer as posted in the
 'va_list / rolling your own printf / Outbuffer' thread. I couldn't
 find any other information on it so there might just be something
 else which would explain for the behaviour).


 import c.stdio;
 import string;

 void Print( char[] msg, ... )
 {
 va_list ap;
 ap = cast(va_list)&msg;
 ap += msg.size;

 char[] buf;
 buf.length = 1024;
 vsprintf(buf, msg, ap);
 buf.length = strlen(buf);

 printf( "%.*s", buf );
 }

 int main( char [] [] args )
 {
 float test = 1.2345;
 Print( "%f\n", 1.2345 );
 Print( "%f\n", test );

 return 1;
 }

 This outputs:

 1.234500
 0.000000

 - Marcel


Oct 25 2003