www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D f(...) to C f(va_list)

reply Denis R <denis_r telkomsa.net> writes:
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 13 2005
next sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"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 13 2005
next sibling parent Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> writes:
-----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
prev sibling parent reply Brian White <bcwhite precidia.com> writes:
 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 16 2005
parent DenisR <DenisR_member pathlink.com> writes:
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.

May 20 2005
prev sibling next sibling parent Denis R <denis_r telkomsa.net> writes:
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
prev sibling parent Denis R <denis_r telkomsa.net> writes:
: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 14 2005