Any way to find # of args in a ...? 
Author Message
 Any way to find # of args in a ...?

Hi there,

I'm wondering if there is a standard (ANSI, or atleast very well
supported) way to find out how many arguments are being passed to a
function which uses the ... (like printf, for example).  Can it be
done?  Can you atleast find out the size of the remaining parameters?
Is there a way to find out if the current parameter returned from va_arg
is the last one?

Lastly... what about a simply GNU solution?  Can the GNU compiler suite
do it?

For an example as to why I'd like to know this, well, I'm trying to
write a function which acts differently based on the number of
parameters it receives, except that the programmer doesn't explicitly
tell the function how many.

Thanks a lot,
Jeff

PS: Please respond via email if possible.  This computer may be going in
for service and so I might not be able to read news, and it'll be
expired by the time I get it back.  Thanks.

--------------------------------------------
            - Code X Software -
       Programming to a Higher Power


        web:   http://www.*-*-*.com/
--------------------------------------------



Thu, 24 Aug 2000 03:00:00 GMT  
 Any way to find # of args in a ...?

Quote:

>Hi there,

>I'm wondering if there is a standard (ANSI, or atleast very well
>supported) way to find out how many arguments are being passed to a
>function which uses the ... (like printf, for example).  Can it be
>done?

No, it is up to you to supply that information. For example printf()
uses the format string to pass the required information.

Quote:
> Can you atleast find out the size of the remaining parameters?

No.

Quote:
>Is there a way to find out if the current parameter returned from va_arg
>is the last one?

No.

Quote:
>Lastly... what about a simply GNU solution?  Can the GNU compiler suite
>do it?

The function call mechanisms used by many platforms (and which gcc has
to conform to) simply don't pass that sort of information.

Quote:
>For an example as to why I'd like to know this, well, I'm trying to
>write a function which acts differently based on the number of
>parameters it receives, except that the programmer doesn't explicitly
>tell the function how many.

The comp.lang.c FAQ list has some information on this. The called function
must be able to determine for itself the number and types of the arguments
passed. That will require some sort of convention being set up between
the function and its callers. printf's format string is one example.
Another simples one is the POSIX.1 exec*() functions that use a marker
(a null pointer) to mark the end of (or a specific point in) the argument
list.

--
-----------------------------------------


-----------------------------------------



Fri, 25 Aug 2000 03:00:00 GMT  
 Any way to find # of args in a ...?

Quote:

> For an example as to why I'd like to know this, well, I'm trying to
> write a function which acts differently based on the number of
> parameters it receives, except that the programmer doesn't explicitly
> tell the function how many.

If you're asking what I think you're asking:

In C++ (and maybe some implementations of C ?) you can overload a
function call to respond differently depending upon how many and what
type of operators are passed to the function.  For example:

int walk( int foo, int foobar, char* bar );
int walk( int foo, int foobar );
int walk( float foo, float foobar, char* bar ); // and
int walk( float foo, int foobar, Array& bar );

are all different function declarations, and therefore can have
different function definitions.  C++ automatically decides which
function your programmer is calling.  When a programmer calls foo, it
choses the best version of foo to use based on the parameters of the
function call.  For example:

foo(6,12,foo);
foo(5,90);
foo(5.2,3.7,foo); // and
foo(2.2,9.9,foo);

all call different functions.

Does this answer your question?

Chris
hempcamp 'at' udel.edu



Fri, 25 Aug 2000 03:00:00 GMT  
 Any way to find # of args in a ...?

Quote:

>Hi there,

>I'm wondering if there is a standard (ANSI, or atleast very well
>supported) way to find out how many arguments are being passed to a
>function which uses the ... (like printf, for example).  Can it be
>done?  Can you atleast find out the size of the remaining parameters?
>Is there a way to find out if the current parameter returned from va_arg
>is the last one?

>Lastly... what about a simply GNU solution?  Can the GNU compiler suite
>do it?

>For an example as to why I'd like to know this, well, I'm trying to
>write a function which acts differently based on the number of
>parameters it receives, except that the programmer doesn't explicitly
>tell the function how many.

Can you use C++? It allows you to make multiple declaration for the same name
and automatically sort them out by matching arguments. I'm not sure how you
can determine how many arguments are in an argument list. Can you just scan
down it, incrementing a counter? I looked through the help for the va_arg
stuff and it doesn't look like it has that kind of facility. I would suggest
either adding an extra flag parameter to specify what kind of arguments you
have or use a different name for each basic pattern.


Fri, 25 Aug 2000 03:00:00 GMT  
 Any way to find # of args in a ...?

Quote:

> Hi there,

> I'm wondering if there is a standard (ANSI, or atleast very well
> supported) way to find out how many arguments are being passed to a
> function which uses the ... (like printf, for example).  Can it be

C++ allows function overloading, so you can write different functions
with the same name, but different parameter lists. Of course all this
works with compile time binding, so you cannot dynamicaly change things.
i.e. mult(int, int) can call a function that multiplies a two ints,
while mult(int, int, int) calls a different function that multiplies
three integers.

If you need too dynamically change the argument list, then the main
function (argc, argv[] ) is a good example of how you can do this.



Fri, 25 Aug 2000 03:00:00 GMT  
 Any way to find # of args in a ...?

Quote:

> I'm wondering if there is a standard (ANSI, or atleast very well
> supported) way to find out how many arguments are being passed to a
> function which uses the ... (like printf, for example).  Can it be
> done?

Count them, like this,

        #include <stdarg.h>

        int count_my_args(char *msgStr, ...)
        {
           int count = 0;
           va_list ap;

           va_start( ap, msgStr);
           while ( va_arg( ap, int) != 0) {
              count++;
           }
           va_end( ap);

           return count;
        }

Quote:
>        Can you atleast find out the size of the remaining parameters?
> Is there a way to find out if the current parameter returned from va_arg
> is the last one?

        At the beginning of your variable argument function using ..., call the
count_my_args() function.  By keeping track of the return value, you can tell
when you reach the last parameter.  Some data types complicate matters, so you
might have to make some adjustments, pass pointers, use ints to index into a
table of data structures, etc., but hopefully this will help you start.

                                                        -- Stone



Sat, 26 Aug 2000 03:00:00 GMT  
 Any way to find # of args in a ...?


Quote:


> > I'm wondering if there is a standard (ANSI, or atleast very well
> > supported) way to find out how many arguments are being passed to a
> > function which uses the ... (like printf, for example).  Can it be
> > done?

> Count them, like this,

>         #include <stdarg.h>

>         int count_my_args(char *msgStr, ...)
>         {
>            int count = 0;
>            va_list ap;

>            va_start( ap, msgStr);
>            while ( va_arg( ap, int) != 0) {
>               count++;
>            }
>            va_end( ap);

>            return count;
>         }

> >        Can you atleast find out the size of the remaining parameters?
> > Is there a way to find out if the current parameter returned from va_arg
> > is the last one?

>         At the beginning of your variable argument function using ...,
call the
> count_my_args() function.  By keeping track of the return value, you can tell
> when you reach the last parameter.  Some data types complicate matters, so you
> might have to make some adjustments, pass pointers, use ints to index into a
> table of data structures, etc., but hopefully this will help you start.

Sorry, but this is complete nonsense. Your function count_my_args will
invoke undefined behavior if any of the actual arguments is not of type
int or unsigned int (after automatic promotions), or if none of the
arguments is zero. If any of the arguments is zero, it will not return the
number of arguments. For example, count_my_args ("", 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0) will return 0.

Furthermore, there is no way to pass all the arguments of one variable
argument function to another.



Sat, 26 Aug 2000 03:00:00 GMT  
 Any way to find # of args in a ...?



Quote:

>> I'm wondering if there is a standard (ANSI, or atleast very well
>> supported) way to find out how many arguments are being passed to a
>> function which uses the ... (like printf, for example).  Can it be
>> done?

>Count them, like this,

>        #include <stdarg.h>

>        int count_my_args(char *msgStr, ...)
>        {
>           int count = 0;
>           va_list ap;

>           va_start( ap, msgStr);
>           while ( va_arg( ap, int) != 0) {
>              count++;
>           }
>           va_end( ap);

>           return count;
>        }

This is only likely to work if the caller passes a sequence of int values
in the variable argument list with the last one being zero (with none of
the others zero). What this certainly isn't is a general way of counting
argument - there is no way to do that in C.

--
-----------------------------------------


-----------------------------------------



Sat, 26 Aug 2000 03:00:00 GMT  
 Any way to find # of args in a ...?

Quote:


> > I'm wondering if there is a standard (ANSI, or atleast very well
> > supported) way to find out how many arguments are being passed to a
> > function which uses the ... (like printf, for example).  Can it be
> > done?

> Count them, like this,

>         #include <stdarg.h>

>         int count_my_args(char *msgStr, ...)
>         {
>            int count = 0;
>            va_list ap;

>            va_start( ap, msgStr);
>            while ( va_arg( ap, int) != 0) {
>               count++;
>            }

No, this will not work for several reasons:
  1) It assumes that all parameters have type "int".
  2) It will stop at the first 0 in the variable parameter list.
     Consider a function call like:
        count_my_args( "Hello", 42, 0, 666 );

Quote:
> >        Can you atleast find out the size of the remaining parameters?
> > Is there a way to find out if the current parameter returned from va_arg
> > is the last one?

>         At the beginning of your variable argument function using ..., call the
> count_my_args() function.  By keeping track of the return value, you can tell
> when you reach the last parameter.

No, you can not. Please try to understand that there is no way of
determining the number or type of variable arguments, without
passing in specific additional information.

"printf()" shows one way of doing it. If you the type of the variable
arguments in advance you can *define* a special stop value, but you
will not be able to use that value as a normal parameter. Or you
must specify the number of variable arguments as a normal function
parameter.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sat, 26 Aug 2000 03:00:00 GMT  
 Any way to find # of args in a ...?


<<snipped>>
: are all different function declarations, and therefore can have
: different function definitions.  C++ automatically decides which
: function your programmer is calling.  When a programmer calls foo, it
: choses the best version of foo to use based on the parameters of the
: function call.  For example:

: foo(6,12,foo);
: foo(5,90);
: foo(5.2,3.7,foo); // and
: foo(2.2,9.9,foo);

It seems to me that the last two calls to foo call the same function, that is,
the overloaded version of foo, which expects float, float, function address as
arguments.



Tue, 29 Aug 2000 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Q on reading command line args OR stdin args

2. Difference between two different ways of doing things

3. 3 ways to uppercase

4. complex numbers - most common ways?

5. Ways to fill in missing code

6. Different ways of implementing a 2d array

7. Help with ideas, best ways etc...

8. Good ways to obfuscate/mangle strings?

9. The Top 10 Ways To Get Screwed by C

10. Different ways of licensing class libraries

11. Need a sponsor in shortest path, optimal route, optimal ways

12. are there better ways to read config file?

 

 
Powered by phpBB® Forum Software