Help with structs

J

Jack Trades

P. S. Do you have a well-rounded middle-name? (Just kidding.)

Sure, the full name is Jack '() Trades.

When I was young I started trading stocks/options and used the alias
Jack Trades as a pun. Though I don't trade much anymore, I still use
the nick.

Jack Trades
 
S

Shao Miller

Jack said:
Sure, the full name is Jack '() Trades.

When I was young I started trading stocks/options and used the alias
Jack Trades as a pun. Though I don't trade much anymore, I still use
the nick.
Aha. I wondered if it might be "Ofall" or "Oval". :)
 
G

Gene

Until a few minutes ago I never knew about #ifndef.  I think I
understand how it works and I'm reading about other preprocessor
directives now, but what does ## do?  I can't seem to find it in any
of the ~10 pages I've read through.

Thanks
Jack Trades- Hide quoted text -

It's called the token splicing operator A ## B turns tokens A and B
into a single token. In this case it splices the token in X to _OBJECT
so you have a single token, e.g. FLONUM_OBJECT. Again, this is
happinging in the space of the C program text before the compiler
begins its work.

If you have not seen it before the #ifndef NDEBUG will look strange.
NDEBUG is a preprocessor variable that's conventionally defined to
turn _off_ error checking code (like assertions; see header
assert.h ) . So #ifndef NDEBUG is a double negative. It says "if
the user has not turned off error checking," or equivalently "if the
user wants error checking" .
 
K

Keith Thompson

Jack Trades said:
[...]> Macros just do text replacement.  The main safety measure is to put
all parameters and the entire result in parens so you don't get
unexpected associativity problems.

[...]

My usual HHGTTG-inspired horrible example is:

#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
    printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
    return 0;

}

Which would become 1+5*8+1 and return 42?

I'm assuming that in addition to order of operations issues there is
also a problem with variable capture as is the case with LISP style
macros? I know this is probably a stupid question but are there any
third-party hygenic macro packages available for C?

I'm not quite sure what "variable capture" is, but I don't think it
applies. Macro expansion is in effect another language layered on top
of C. The preprocessor has no awareness of C expressions; it operates
entirely on sequences of tokens. The macro SIX expands to a sequence of
three tokens, ``1'', ``+'', and ``5''; similarly for NINE. A later
phase of translation then sees the resulting token sequence:

printf ( "%d * %d = %d\n" , 1 + 5 , 8 + 1 , 1 + 5 * 8 + 1 ) ;

I'm not sure what a "third-party hygenic macro package" would look like.
When you're writing and using macros, you just have to be careful to
follow a few guidelines. Use all-caps names as a reminder that the
thing you're using is a macro and not a function. For macros to be used
in place of expressions, make sure each argument reference is
parenthesized, as is the entire definition. For example:

#define SQUARE(x) ((x) * (x))

For macros that play tricks with the syntax, it's not always so simple,
and you have to be *really* careful.
 
N

Nick

Behind China Blue Eyes said:
You can also use a bit vector version of a semilattice to reduce
switches.

And you can create a table of function pointers. This is quite useful
if many of your types can't be interconverted. You can even combine
this with some macro-preprocessory to define which interconversions are
possible.

So you'd have a three-dimensional table for operations (operation, type
of first argument, type of second argument) and a pile of functions
called:
add_fixed_fixed
add_fixed_float
add_float_fixed
add_float_float
....
mult_float_float

then you just get the operation and the two types and call the
appropriate function.

I've got the double nested switch for coercion, and the function table
for functions in my interpreter. Both are pretty horrid - this really is
something that there doesn't seem to be an elegant way to do.
 

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,792
Messages
2,569,639
Members
45,351
Latest member
RoxiePulli

Latest Threads

Top