Thread overview
D f(...) to C f(va_list)
May 14, 2005
Denis R
May 14, 2005
Ben Hinkle
May 14, 2005
Denis R
May 14, 2005
Thomas Kuehne
May 14, 2005
Denis R
May 16, 2005
Brian White
May 20, 2005
DenisR
May 14, 2005
Hello,

Im having trouble with sending variable number of args that my D funcs gets, into a C func, that takes va_list as one of the args.

Im not too sure how this is suppose to be done. I guess in same way as its done in C, but using those std.c.stdarg's templates ?
Anyone could give me a quick example ? Im not good with D yet :)
May 14, 2005
"Denis R" <denis_r@telkomsa.net> wrote in message news:20050514021847.3425372d.denis_r@telkomsa.net...
> Hello,
>
> Im having trouble with sending variable number of args that my D funcs gets, into a C func, that takes va_list as one of the args.
>
> Im not too sure how this is suppose to be done. I guess in same way as its
> done in C, but using those std.c.stdarg's templates ?
> Anyone could give me a quick example ? Im not good with D yet :)

If the C function is something like
  extern (C) vfoo(va_list args);
then you should be able to do
  void bar(...) {
   vfoo(_argptr);
  }

If not then something is fishy.


May 14, 2005
On Fri, 13 May 2005 22:00:19 -0400
"Ben Hinkle" <ben.hinkle@gmail.com> wrote:

> 
> "Denis R" <denis_r@telkomsa.net> wrote in message news:20050514021847.3425372d.denis_r@telkomsa.net...
> > Hello,
> >
> > Im having trouble with sending variable number of args that my D funcs gets, into a C func, that takes va_list as one of the args.
> >
> > Im not too sure how this is suppose to be done. I guess in same way as its
> > done in C, but using those std.c.stdarg's templates ?
> > Anyone could give me a quick example ? Im not good with D yet :)
> 
> If the C function is something like
>   extern (C) vfoo(va_list args);
> then you should be able to do
>   void bar(...) {
>    vfoo(_argptr);
>   }
> 
> If not then something is fishy.
> 
> 

This example doest work :/  I get segfault right on that vfprintf call

extern (C) int vfprintf(FILE *stream, char *format, va_list ap);
void myfunc(char *fmt, ...)
{
  puts("hello");
  vfprintf(stderr, fmt, _argptr);
}
int main (char[][] args)
{
  myfunc("%s\n", "Bla bla blas");
  return (0);
}

(By the way, why cant I have const char* ptr ?  I thought it was allowed. )
May 14, 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Denis R schrieb am Sat, 14 May 2005 09:25:11 +0200:
>
> This example doest work :/  I get segfault right on that vfprintf call
>
> extern (C) int vfprintf(FILE *stream, char *format, va_list ap);
> void myfunc(char *fmt, ...)
> {
>   puts("hello");
>   vfprintf(stderr, fmt, _argptr);
> }
> int main (char[][] args)
> {
>   myfunc("%s\n", "Bla bla blas");

	myfunc("%.*s\n", "Bla bla blas");
or
	myfunc("%s\n", std.string.toStringz("Bla bla blas"));

>   return (0);
> }

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFChcby3w+/yD4P9tIRAggXAJ9BWCGz7o0tM0wacjv8s0UB0Rw5ZwCgpIBg
SBmuLgYtLINHFTazdqWPkUU=
=vxSp
-----END PGP SIGNATURE-----
May 14, 2005
:D

That seems to work. Thank you.

Will try now on my actual code.


On Sat, 14 May 2005 11:37:54 +0200
Thomas Kuehne <thomas-dloop@kuehne.thisisspam.cn> wrote:

> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Denis R schrieb am Sat, 14 May 2005 09:25:11 +0200:
> >
> > This example doest work :/  I get segfault right on that vfprintf call
> >
> > extern (C) int vfprintf(FILE *stream, char *format, va_list ap);
> > void myfunc(char *fmt, ...)
> > {
> >   puts("hello");
> >   vfprintf(stderr, fmt, _argptr);
> > }
> > int main (char[][] args)
> > {
> >   myfunc("%s\n", "Bla bla blas");
> 
> 	myfunc("%.*s\n", "Bla bla blas");
> or
> 	myfunc("%s\n", std.string.toStringz("Bla bla blas"));
> 
> >   return (0);
> > }
> 
> Thomas
> 
> 
> -----BEGIN PGP SIGNATURE-----
> 
> iD8DBQFChcby3w+/yD4P9tIRAggXAJ9BWCGz7o0tM0wacjv8s0UB0Rw5ZwCgpIBg
> SBmuLgYtLINHFTazdqWPkUU=
> =vxSp
> -----END PGP SIGNATURE-----
May 16, 2005
> This example doest work :/  I get segfault right on that vfprintf call
> 
> extern (C) int vfprintf(FILE *stream, char *format, va_list ap);
> void myfunc(char *fmt, ...)
> {
>   puts("hello");
>   vfprintf(stderr, fmt, _argptr);
> }
> int main (char[][] args)
> {
>   myfunc("%s\n", "Bla bla blas");
>   return (0);
> }

I believe that D is passing your argument string as a "char[]" and not a "char*".  The former is actually two values, a element count and a pointer (in that order).  Thus, the %s tries to dereference the element count as a pointer and goes in to neverland.

You could try: cast(char*)"Bla bla blas"  (I haven't tried it -- I'm just speculating).


> (By the way, why cant I have const char* ptr ?  I thought it was allowed. )

No, D uses as a scope rather than as an attribute (or some wording like that).  The upshot is that it can't be used to describe constness of something being pointed to.  Personally, I think this is a grave deficiency in the language, but I'm just a novice at D and may simply not undestand the genius of the choice.  <grin>

                                          Brian
                                 ( bcwhite@precidia.com )

-------------------------------------------------------------------------------
    In theory, theory and practice are the same.  In practice, they're not.
May 20, 2005
Hey,

I did try the way you suggest. But the cast did not work.

Did you see the second reply I got ?  You just specify "%*.s", and that kind of makes D's char[] into the C's char* :)




In article <d6als4$28lj$1@digitaldaemon.com>, Brian White says...
>
>> This example doest work :/  I get segfault right on that vfprintf call
>> 
>> extern (C) int vfprintf(FILE *stream, char *format, va_list ap);
>> void myfunc(char *fmt, ...)
>> {
>>   puts("hello");
>>   vfprintf(stderr, fmt, _argptr);
>> }
>> int main (char[][] args)
>> {
>>   myfunc("%s\n", "Bla bla blas");
>>   return (0);
>> }
>
>I believe that D is passing your argument string as a "char[]" and not a "char*".  The former is actually two values, a element count and a pointer (in that order).  Thus, the %s tries to dereference the element count as a pointer and goes in to neverland.
>
>You could try: cast(char*)"Bla bla blas"  (I haven't tried it -- I'm just speculating).
>
>
>> (By the way, why cant I have const char* ptr ?  I thought it was allowed. )
>
>No, D uses as a scope rather than as an attribute (or some wording like that).  The upshot is that it can't be used to describe constness of something being pointed to.  Personally, I think this is a grave deficiency in the language, but I'm just a novice at D and may simply not undestand the genius of the choice.  <grin>
>
>                                           Brian
>                                  ( bcwhite@precidia.com )
>
>-------------------------------------------------------------------------------
>     In theory, theory and practice are the same.  In practice, they're not.