Alex said:
Thanks for the reply!
How do you determine when you've reached the end of the arguement list?
say you parse 5 arguements to a function, but the code in the function is
processing
each arguement till it reaches the end, how could code be written to check
if the arguement
being read, is the last one?
Please do not top-post. Write your answer below the text you are
referring to.
There are essentially 3 ways to communicate the number of arguments
- explicit: one of the mandatory arguments is the number of
(optional or total) arguments. Example:
double sumN (int N, ... /* doubles */)
{
/* N>=0, sums up the optional arguments which are all
** of type double*/
}
- semi-explicit: one of the mandatory arguments contains
information which gives you the number of optional arguments.
Example:
int printf(const char*restrict format, ...)
{
/* Note: restrict is a C99 keyword */
}
- implicit: the last argument has a certain form which tells
you that it is the last. Example:
/* UNIX system call for a program, <unistd.h> */
int execl(const char *path, const char *arg, ... /* strings */);
/* Explanation from the c.l.c FAQ 5.2:
For example, the Unix system call
execl takes a variable-length, null-pointer-terminated list of
character pointer arguments, and is correctly called like this:
execl("/bin/sh", "sh", "-c", "date", (char *)0);
That is, the last argument is a null pointer of type (char *).
*/
Note that in the first and last case, we restricted ourselves to
a certain type for the variable arguments. The format string of
printf also gives us the flexibility for arbitrary argument types.
You can extend and/or combine the three ideas, but in the end, it
comes down to those.
Cheers
Michael