Struct member access with macro

Discussion in 'C Programming' started by dam_fool_2003@yahoo.com, Sep 29, 2003.

  1. Guest

    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?
     
    , Sep 29, 2003
    #1
    1. Advertising

  2. -berlin.de Guest

    wrote:
    > 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
    --
    _ _____ _____
    | ||_ _||_ _| -berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oerring
     
    -berlin.de, Sep 29, 2003
    #2
    1. Advertising

  3. Dan Pop Guest

    In <> writes:

    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 29, 2003
    #3
  4. Eric Sosman Guest

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

    --
     
    Eric Sosman, Sep 29, 2003
    #4
  5. On 29 Sep 2003 06:03:48 -0700, in comp.lang.c ,
    wrote:

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

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
     
    Mark McIntyre, Sep 29, 2003
    #5
  6. CBFalconer Guest

    -berlin.de wrote:
    > wrote:
    >
    > > Several advanced programs access the struct's member like the
    > > one following:

    >
    > > #define str(p) (p.data)
    > > struct node
    > > {
    > > unsigned int data;
    > > };

    >

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

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Sep 30, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angus Comber
    Replies:
    7
    Views:
    1,198
    Richard Heathfield
    Feb 5, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,438
    Chris Fogelklou
    Apr 20, 2004
  3. dutchgoldtony
    Replies:
    15
    Views:
    492
    Mike Wahler
    Nov 16, 2005
  4. pete142
    Replies:
    5
    Views:
    465
    Ben Bacarisse
    Jan 23, 2008
  5. Mark

    macro to access structure's member

    Mark, Oct 17, 2011, in forum: C Programming
    Replies:
    8
    Views:
    1,156
Loading...

Share This Page