Determining Number of Arguments

T

Trevor M. Lango

The number of arguments passed to the following functions can be
determined - but how is this coded?

main (int argc, char *argv[])
printf ( string format [, mixed args])

I would like to be able to write arbitrary functions that know how many
arguments were passed to them.
 
R

Rolf Magnus

Trevor said:
The number of arguments passed to the following functions can be
determined - but how is this coded?

main (int argc, char *argv[])

main is a special function. You're not allowed to call it.
printf ( string format [, mixed args])

I would like to be able to write arbitrary functions that know how
many arguments were passed to them.

This is a function with a variable argument list. It's declared
something like:

int printf(const char* format, ...);

The ellipsis (...) indicates a variable list of arguments. The actual
number of arguments is determined from the format string. You have to
provide your own way for the function to find out the number and type
of arguments if you want to use variable argument lists. This is one
reason why you actually shouldn't do that. It's not type-safe and can
easiliy lead to errors that the compiler cannot detect. Another
disadvantage is that you can only pass POD types. Alternatively, you
can write a function or operator that 'writes' to an object and returns
a reference to that object, so that calls to that funciton/operator can
be stacked together. This is e.g. done in the C++ stream classes and
their operator<< and >>.
 
J

Jack Klein

The number of arguments passed to the following functions can be
determined - but how is this coded?

main (int argc, char *argv[])
printf ( string format [, mixed args])

I would like to be able to write arbitrary functions that know how many
arguments were passed to them.

The only response I see so far just tells you that "you shouldn't do
it". But it is perfectly legal in C and C++, and the languages
provide standard features to do so.

See http://www.eskimo.com/~scs/C-faq/s15.html in the FAQ for
comp.lang.c for a starter on using <stdarg.h> or <cstdarg>.
 
R

Rolf Magnus

Jack said:
The number of arguments passed to the following functions can be
determined - but how is this coded?

main (int argc, char *argv[])
printf ( string format [, mixed args])

I would like to be able to write arbitrary functions that know how
many arguments were passed to them.

The only response I see so far just tells you that "you shouldn't do
it".

No, I also gave some of the reasons why "you shouldn't do it". I also
told how to declare a function with variable argument lists. I just
But it is perfectly legal in C and C++, and the languages
provide standard features to do so.

I never claimed anything else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top