Way to determine type of variable?

  • Thread starter Walter L. Preuninger II
  • Start date
S

Sam Dennis

Eric said:
Sam said:
Walter said:
void xerror( const char *fmt, ... )
{
/* Put printing argv[0], __FILE__ and __LINE__ in here */
[You can't.]

#define xerror( fmt, ... ) xerror( "%s:%d - " fmt, __FILE__, __LINE, \
__VA_ARGS__ )

However, this has several limitations (other than requiring C99 macros)
so I'm sure that you'll want to try to make something better.

#define XERROR xwhere(__FILE__, __LINE__) , xerror

Almost precisely what I was thinking of, but I wanted to leave something
for the OP to do.
 
K

Kenny McCormack

Eric Sosman said:
The fundamental problem is that C is a statically-typed
language, meaning that the type of every expression is fixed
at compile time. True, <stdarg.h> (not <varargs.h>, BTW) can
evade this requirement, but only briefly: In order to access
the arguments corresponding to `...', you must use an expression
whose type is known and immutable. C is not Lisp.

Funny you should mention that - and in just that way.

<rant>
WARNING WARNING WARNING - OFF TOPIC OFF TOPIC OFF TOPIC (*)
</rant>

If you google to the right place, you'll find the following text posted by
one K. McCormack at some point in the not so distant past:

<text from google>
The answer is a neat little package called "ffcall", which came out of the
Common Lisp effort, and allows you to setup calls to variadic functions at
something like the assembler level - you basically are pushing args onto
the stack, in a loop. The ffcall package contains 4 related things - the
only one you need for my version of strfmon() is the "avcall" package.

You can get ffcall from: ftp://ftp.santafe.edu/pub/gnu/ffcall-1.6.tar.gz
</text from google>

Note that ffcall is described as "var args in reverse", but I think of it
as "var args at runtime". Note that var args is all compile-time.

The point is that *it* *allows* you to write a C program that works like
this:

:loop
Enter a type and a value...
goto :loop

After exiting loop (running the loop an arbitrary number of times), call
printf, and print out all the values with the right types. That's
something you can't do with var args.

(*) Although, as an aside, is it really OT (and thus an unforgivable sin)
to state that "X can't be done in Standard C, but here's a pointer
a library (or similar external facility) that allows you to do what you
want" ?
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top