Struct member access with macro

D

dam_fool_2003

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?
 
J

Jens.Toerring

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 I can see at the moment. It just helps obfuscate what the program
is doing. And you should use "%d" with unsigned integers, that's what
"%u" is for. And using "str" is something you should avoid because
names starting with "str" are reserved for the implementation.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
D

Dan Pop

In said:
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?

No real advantage in this example: it saves some typing (if str(p) is
used often enough) at the expense of rendering the code less readable.

The real advantage comes when both the macro definition and the struct
definition come from an external header and the struct is used as an
abstract data type. In this case, you can access a bit of information
stored in the struct, without having any other information about the
struct.

A typical real world example is the Unix fileno(), often implemented as
a macro (because the file descriptor number is usually a member of the
FILE struct).

Dan
 
E

Eric Sosman

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.)
 
M

Mark McIntyre

On 29 Sep 2003 06:03:48 -0700, in comp.lang.c ,
Hai,
Several advanced programs access the struct's member like the one following:

#define str(p) (p.data) ....
What is the advantage of doing so?

The only reason for doing this would be if you wanted to hide the
definition of p from the program using it. Its a kind of poor man's
private data. IF you did this, then you'd normally put the definition
of p and the macro in a header.
 
C

CBFalconer

.... snip ...

And using "str" is something you should avoid because names
starting with "str" are reserved for the implementation.

Only if followed by a lower case letter. This case is OK.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top