da***********@yahoo.com wrote:
Hai,
Several advanced programs access the struct's member like the one following:
#define str(p) (p.data)
struct node
{
unsigned int data;
};
int main(void)
{
struct node p;
p.data = 10;
printf("%d\n",str(p));
return 0;
}
The above is just a basic demo and it is not the only thing around
What is the advantage of doing so?
None, in situations as simple as this sample. However,
there can be a benefit in more complex situations. One I ran
across involved a document formatting system that kept track
of a large number of attributes for each paragraph, each word,
and so on. Some of the attributes were used very infrequently,
and it proved worthwhile not to store all of them explicitly
unless they had non-default values. So the code looked like
(*very* inaccurate paraphrase):
struct paragraph {
int this;
int that;
double the_other;
struct rare_attrs *rare;
};
struct rare_attrs {
enum { WEIRD_TYPE, STRANGE_TYPE } attr_type;
int attr_value;
struct rare_attrs *next;
};
#define THIS(p) (p).this
#define THAT(p) (p).that
#define THE_OTHER(p) (p).the_other
#define WEIRD(p) ((p)->rare ? compute_weird(p) : WEIRD_DEFAULT)
#define STRANGE(p) ((p)->rare ? compute_strange(p) : STRANGE_DEFAULT)
.... and so on. The user of WEIRD and STRANGE didn't need to
know whether these attributes were stored right there in the
paragraph struct or were somewhere in an "exceptionals" list
chained from it. Furthermore, if it later turned out that
WEIRD was in actual practice more common than originally
thought, it could be "promoted" into the paragraph struct with
a simple adjustment of the macro, and none of the code that
used it needed to change.
Another use is to hide irrelevant intermediate names, often
in connection with the use of unions:
union flags {
struct {
unsigned int f1 : 1;
unsigned int f2 : 1;
unsigned int f3 : 1;
} s;
unsigned int all_flags;
};
#define F1(p) (p).s.f1
#define F2(p) (p).s.f2
#define F3(p) (p).s.f3
#define ALL(p) (p).all_flags
The intermediate name `s' is just an encumbrance, and could
be done away with if only C would permit anonymous elements (some
compilers support this as a non-conforming extension to the
language). The macros allow the meaningless name to be hidden --
not much of a gain in this simple illustration, but the value
becomes more apparent as the number of such unions within a
single enclosing struct increases.
(By the way, the correspondence between f1/f2/f3 and the bit
positions in all_flags is implementation-defined, and it could
even be the case that some of the flags lie entirely outside
the all_flags element. This technique relies on non-portable
knowledge about the implementation at hand.)
--
Er*********@sun.com