Variable arg function question

2008-05-04 Thread Unga
Hi all

I need to implement a variable argument function in C.
The number of args are not known but the type is
known, all are strings.

Unfortunately va_arg() [stdarg(3)] does not return
NULL or any other suitable value after processing the
arg list, it just simply crashes once the arg list is
exhausted.

It seems there is no way to know the number of args
inside the called function.

How do you guys implement variable arg function such
as f(str1, str2, str3, ..., strN)?

Sorry for the sightly off topic question, the only
relevance is I'm programming this app on FreeBSD 7.0
:)

Many thanks in advance.

Kind regards
Unga




  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Variable arg function question

2008-05-04 Thread Roland Smith
On Sun, May 04, 2008 at 12:40:43AM -0700, Unga wrote:
 Hi all
 
 I need to implement a variable argument function in C.
 The number of args are not known but the type is
 known, all are strings.
 
 Unfortunately va_arg() [stdarg(3)] does not return
 NULL or any other suitable value after processing the
 arg list, it just simply crashes once the arg list is
 exhausted.

It is _your_ task to properly close the argument list. E.g. by supplying
a NULL pointer as the last argument.
 
 It seems there is no way to know the number of args
 inside the called function.

Not with stdarg.

 How do you guys implement variable arg function such
 as f(str1, str2, str3, ..., strN)?

you could use the same format as main: int foo(int num, char **args)

Roland
-- 
R.F.Smith   http://www.xs4all.nl/~rsmith/
[plain text _non-HTML_ PGP/GnuPG encrypted/signed email much appreciated]
pgp: 1A2B 477F 9970 BA3C 2914  B7CE 1277 EFB0 C321 A725 (KeyID: C321A725)


pgp8rpDPLi4A8.pgp
Description: PGP signature


Re: Variable arg function question

2008-05-04 Thread Unga

--- Roland Smith [EMAIL PROTECTED] wrote:

 On Sun, May 04, 2008 at 12:40:43AM -0700, Unga
 wrote:
  Hi all
  
  I need to implement a variable argument function
 in C.
  The number of args are not known but the type is
  known, all are strings.
  
  Unfortunately va_arg() [stdarg(3)] does not return
  NULL or any other suitable value after processing
 the
  arg list, it just simply crashes once the arg list
 is
  exhausted.
 
 It is _your_ task to properly close the argument
 list. E.g. by supplying
 a NULL pointer as the last argument.
  
Infact, I have implemented it in this way. I was
wondering if there is a better way. The issue I see
is, if someone forget to close the arg list with NULL
pointer, it crashes. 


  How do you guys implement variable arg function
 such
  as f(str1, str2, str3, ..., strN)?
 
 you could use the same format as main: int foo(int
 num, char **args)
 

This is interesting. Who set the num? The compiler or
the user. If it is the user, its no better than above
NULL pointer method.

If this is possible, my problem is solved:
f(str1, str2, str3, ..., strN) is at compile time
expands to _f(int num, str1, str2, str3, ..., strN).

The num is set automatically by the compiler by
counting the args.

Is this possible?

Unga


  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Variable arg function question

2008-05-04 Thread Roland Smith
On Sun, May 04, 2008 at 04:01:39AM -0700, Unga wrote:
   Unfortunately va_arg() [stdarg(3)] does not return
   NULL or any other suitable value after processing
  the
   arg list, it just simply crashes once the arg list
  is
   exhausted.
  
  It is _your_ task to properly close the argument
  list. E.g. by supplying
  a NULL pointer as the last argument.
   
 Infact, I have implemented it in this way. I was
 wondering if there is a better way.

Not really within the bounds of the C language.

   How do you guys implement variable arg function
  such
   as f(str1, str2, str3, ..., strN)?
  
  you could use the same format as main: int foo(int
  num, char **args)
  
 
 This is interesting. Who set the num? The compiler or
 the user. If it is the user, its no better than above
 NULL pointer method.
 
 If this is possible, my problem is solved:
 f(str1, str2, str3, ..., strN) is at compile time
 expands to _f(int num, str1, str2, str3, ..., strN).
 
 The num is set automatically by the compiler by
 counting the args.
 
 Is this possible?

You could write a custom preporcessor that translates f calls into calls
for _f.

Roland
-- 
R.F.Smith   http://www.xs4all.nl/~rsmith/
[plain text _non-HTML_ PGP/GnuPG encrypted/signed email much appreciated]
pgp: 1A2B 477F 9970 BA3C 2914  B7CE 1277 EFB0 C321 A725 (KeyID: C321A725)


pgpSHdHk0d8UA.pgp
Description: PGP signature


Re: Variable arg function question

2008-05-04 Thread Peter Boosten

Unga wrote:

Hi all

I need to implement a variable argument function in C.
The number of args are not known but the type is
known, all are strings.

Unfortunately va_arg() [stdarg(3)] does not return
NULL or any other suitable value after processing the
arg list, it just simply crashes once the arg list is
exhausted.

It seems there is no way to know the number of args
inside the called function.


Why is it in your opinion so hard to count the number of arguments
*before* you call the function, in other words, what in your program
prevents this count?

Peter
--
http://www.boosten.org
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Variable arg function question

2008-05-04 Thread Patrick Clochesy
What about using a macro (...) in front of the function to csll it  
which passes __VARARGS__, NULL to ensure there is always a trailing  
NULL? I think this would at least work in GCC... Can' test on my phone  
though.


-Patrick

On May 4, 2008, at 4:42 AM, Peter Boosten [EMAIL PROTECTED] wrote:


Unga wrote:

Hi all
I need to implement a variable argument function in C.
The number of args are not known but the type is
known, all are strings.
Unfortunately va_arg() [stdarg(3)] does not return
NULL or any other suitable value after processing the
arg list, it just simply crashes once the arg list is
exhausted.
It seems there is no way to know the number of args
inside the called function.


Why is it in your opinion so hard to count the number of arguments
*before* you call the function, in other words, what in your program
prevents this count?

Peter
--
http://www.boosten.org
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED] 


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Variable arg function question

2008-05-04 Thread Roland Smith
On Sun, May 04, 2008 at 07:02:36AM -0700, Patrick Clochesy wrote:
 What about using a macro (...) in front of the function to csll it which 
 passes __VARARGS__, NULL to ensure there is always a trailing NULL? I think 
 this would at least work in GCC... Can' test on my phone though.

That's a good idea. If one uses __VA_ARGS__ instead of __VARARGS__, it
should work with any C99 compliant compiler, including gcc.

The good thing about variadic macros in C99[1] is that you don't need a
first argument.

Roland

[1: http://en.wikipedia.org/wiki/Variadic_macro]
-- 
R.F.Smith   http://www.xs4all.nl/~rsmith/
[plain text _non-HTML_ PGP/GnuPG encrypted/signed email much appreciated]
pgp: 1A2B 477F 9970 BA3C 2914  B7CE 1277 EFB0 C321 A725 (KeyID: C321A725)


pgpJYoWnrc9Ch.pgp
Description: PGP signature


Re: Variable arg function question

2008-05-04 Thread Unga

--- Roland Smith [EMAIL PROTECTED] wrote:

 On Sun, May 04, 2008 at 07:02:36AM -0700, Patrick
 Clochesy wrote:
  What about using a macro (...) in front of the
 function to csll it which 
  passes __VARARGS__, NULL to ensure there is always
 a trailing NULL? I think 
  this would at least work in GCC... Can' test on my
 phone though.
 
 That's a good idea. If one uses __VA_ARGS__ instead
 of __VARARGS__, it
 should work with any C99 compliant compiler,
 including gcc.
 
 The good thing about variadic macros in C99[1] is
 that you don't need a
 first argument.
 
 Roland
 
 [1: http://en.wikipedia.org/wiki/Variadic_macro]
 -- 

I gave it a try, but I cannot get it to work:
(As per above wikipedia example)

void realdprintf (char const *file, int line, char
const *fmt, ...); 
#define dprintf(...) realdprintf(__FILE__, __LINE__,
__VA_ARGS__)

To solve my problem, I must be able to indicate the
end of the arg list, may be by a empty string (),
but GNU C compiler does not allow to specify anything
after the ... .

How do I specify end of arg list? or is that the way?

Unga



  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Variable arg function question

2008-05-04 Thread Roland Smith
On Sun, May 04, 2008 at 08:34:30AM -0700, Unga wrote:
 
 --- Roland Smith [EMAIL PROTECTED] wrote:
 
  On Sun, May 04, 2008 at 07:02:36AM -0700, Patrick
  Clochesy wrote:
   What about using a macro (...) in front of the
  function to csll it which 
   passes __VARARGS__, NULL to ensure there is always
  a trailing NULL? I think 
   this would at least work in GCC... Can' test on my
  phone though.
  
  That's a good idea. If one uses __VA_ARGS__ instead
  of __VARARGS__, it
  should work with any C99 compliant compiler,
  including gcc.

 I gave it a try, but I cannot get it to work:
 (As per above wikipedia example)
 
 void realdprintf (char const *file, int line, char
 const *fmt, ...); 
 #define dprintf(...) realdprintf(__FILE__, __LINE__,
 __VA_ARGS__)
 
 To solve my problem, I must be able to indicate the
 end of the arg list, may be by a empty string (),
 but GNU C compiler does not allow to specify anything
 after the ... .

Try something like what Patrick suggested:

#define f(...) _f(__VA_ARGS__,NULL)

Roland
-- 
R.F.Smith   http://www.xs4all.nl/~rsmith/
[plain text _non-HTML_ PGP/GnuPG encrypted/signed email much appreciated]
pgp: 1A2B 477F 9970 BA3C 2914  B7CE 1277 EFB0 C321 A725 (KeyID: C321A725)


pgp9mvXJN9jch.pgp
Description: PGP signature


Re: Variable arg function question [SOLVED]

2008-05-04 Thread Unga

--- Roland Smith [EMAIL PROTECTED] wrote:

 On Sun, May 04, 2008 at 08:34:30AM -0700, Unga
 wrote:
  
  --- Roland Smith [EMAIL PROTECTED] wrote:
  
   On Sun, May 04, 2008 at 07:02:36AM -0700,
 Patrick
   Clochesy wrote:
What about using a macro (...) in front of the
   function to csll it which 
passes __VARARGS__, NULL to ensure there is
 always
   a trailing NULL? I think 
this would at least work in GCC... Can' test
 on my
   phone though.
   
   That's a good idea. If one uses __VA_ARGS__
 instead
   of __VARARGS__, it
   should work with any C99 compliant compiler,
   including gcc.
 
  I gave it a try, but I cannot get it to work:
  (As per above wikipedia example)
  
  void realdprintf (char const *file, int line, char
  const *fmt, ...); 
  #define dprintf(...) realdprintf(__FILE__,
 __LINE__,
  __VA_ARGS__)
  
  To solve my problem, I must be able to indicate
 the
  end of the arg list, may be by a empty string
 (),
  but GNU C compiler does not allow to specify
 anything
  after the ... .
 
 Try something like what Patrick suggested:
 
 #define f(...) _f(__VA_ARGS__,NULL)
 

Hey, it worked :) Thanks guys for the help. Appreciate
it very much.

Unga


  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]