Re: function definition



Tinkertim <tinkertim@xxxxxxxxx> writes:
On Oct 1, 1:01 pm, Keith Thompson <ks...@xxxxxxx> wrote:
The point is that a printf-style format string isn't the only way to
determine the number and type of arguments that are supposed to be
passed (though I think it's the only method used by any of the
functions in the standard library).

Your correct on the latter, I took a stroll through a few libc
implementations and only saw it in the printf core or things that use
it.

For example, I've seen variadic
functions that take a series of char* arguments, with the end of the
list marked by a (char*)NULL argument.

In that example, the function assumes all arguments to be char * type,
yes? You know your done when you hit a null argument. I'm not sure how
you could mix types without passing some kind of definition of the
incoming format.

I wasn't saying that a format was needed in all cases, just in cases
where arguments are of mixed types .. or is that what you were saying
that you've seen done? Sorry if I'm a little dense today :)

A format *string* is just one of many possible ways to specify the
arguments. Here's a rather contrived example:

#include <stdarg.h>
#include <stdio.h>

enum arg_type { INT, DOUBLE, STRING, END };

void func(enum arg_type first, ...)
{
va_list ap;
enum arg_type next_arg_type = first;
va_start(ap, first);
do {
switch(next_arg_type) {
case INT: {
int i = va_arg(ap, int);
printf("int: %d\n", i);
break;
}
case DOUBLE: {
double d = va_arg(ap, double);
printf("double: %g\n", d);
break;
}
case STRING: {
char *s = va_arg(ap, char*);
printf("string: \"%s\"\n", s);
break;
}
case END: {
/* nothing */
}
}
next_arg_type = va_arg(ap, enum arg_type);
} while (next_arg_type != END);
va_end(ap);
}

int main(void)
{
func(INT, 42, STRING, "hello", DOUBLE, 1.23, END);
return 0;
}

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Using References to Formats, Examining Scalars With Devel::Peek
    ... the format to be able to refer to it by another name didn't occur to me. ... Seems most of the fields are the same as for the rest of the Perl ... from integer to double and how frequently the string portions are updated. ... Using an operator or a built-in with a double and an int usually converts ...
    (comp.lang.perl.misc)
  • Re: [PATCH]: Cleanup: Remove gcc format string warnings when compiling with -Wformat-securit
    ... > posted it's presumably no problem, but if the string could either a) ... > printk format characters then this code has a risk that things could ... int found = 0; ... *dev, struct device *device) ...
    (Linux-Kernel)
  • Re: Parsing strings to other datatypes without throwing exceptions
    ... int month = int.Parse); ... bool isDate; ... > I want to be able to check that a string is of a desired format ... > VB IsDatefunction doesn't work becuase it doesn't know what format ...
    (microsoft.public.dotnet.framework)
  • Re: another printf question!!
    ... > int factorial(int); ... It's best to use fflushafter a printf call, the format string ... Don't include whitespace characters at the end of a scanf format string. ...
    (comp.lang.c)
  • Re: C Strings
    ... saying "C has string objects but not string types", ... So what you're saying is that if you take strings away, ... int *ch; ...
    (comp.lang.c)