Context Variables
Jeffery A. Cavallaro
jeff at isi-vaxa.arpa
Tue Dec 3 09:26:36 AEST 1985
Recent discussion has pointed out that sizeof(int) does not necessarily
== sizeof(char) (what an understatement, blood is still dripping from my
VT100...)
But, is proper to assume:
sizeof(long) >= sizeof(any scalar data type or pointer)
Yes, I know you can use a union, but unions have this problem of not being
able to be initialized.
If this is OK, it is a very handy way to maintain a context variable to
ensure reentrancy and other such things.
(My specific example...)
I am coding a generic capabilities file parser. Each capability item in a
definitions is to be described as follows:
struct capitem {
char *name; /* Name of item */
short type; /* BOOLEAN, STRING, or INTEGER */
long value; /* Discovered value */
long defval; /* Default value */
}
The value and defval fields would be able to hold a Boolean, char *, or
integer value. When declaring an input buffer, then default values
could be statically set, and can be assumed to never change. If this use
of long is not valid and unions must be used, then a routine would be
needed to set the default values for each new instance of a buffer.
I realize that there are other solutions to this problem, but if this use
of long is valid, I like this the best.
Jeff
More information about the Comp.lang.c
mailing list