Re: What is better and why?

Discussion in 'C Programming' started by Lew Pitcher, Jun 10, 2013.

  1. Lew Pitcher

    Lew Pitcher Guest

    On Sunday 09 June 2013 16:57, in comp.lang.c, wrote:

    (implied from subject: Which of these four constructs is "better", and why?)

    > 1st:
    >
    > struct person {
    > char *name;
    > char *surname;
    > };
    >
    > 2nd:
    >
    > typedef struct {
    > char *name;
    > char *surnname;
    > } person;
    >
    > 3th:
    >
    > typedef struct person {
    > char *name;
    > char *surname;
    > } person;
    >
    > 4th:
    >
    > typedef struct _PERSON {
    > char *name;
    > char *surnmae;
    > } person;
    >
    > ?


    I would have to say that they are /all/ "better", in that they all have
    their own unique advantages that would elevate their importance under
    certain circumstances.

    Your first form
    struct person {
    char *name;
    char *surname;
    };
    is basic; it works under all circumstances.
    It has the advantage of being both the simplest form (thus making it's use
    as foolproof as it /can/ be, for a structure), and obvious. It permits
    expansion of the structure to include recursive pointers
    (i.e. { struct person *nextperson; char *name; char *surname; } )
    and does not confuse the programmer with multiple identical names in
    different namespaces. Additionally, it can be used (in additional
    statements) to seed a typedef, if required.

    It has the disadvantage of being "wordier" than the other variations, and of
    exposing the details of the structure, perhaps unnecessarily.


    Your second form
    typedef struct {
    char *name;
    char *surnname;
    } person;
    has the advantages of data hiding, simple namespace, and simplicity of use.
    However, as an anonymous structure, access to its members will always be
    through a typedef'ed declaration; you will not be able to declare /this/
    structure in any other way (/similar, identically populated/ structures are
    possible, but the compiler will not recognize them as identical to /this/
    structure; anonymous structures are always "unique"). Additionally, this
    form may not be modified to include a recursive pointer
    (i.e. { person *nextperson; char *name; char *surname; } )


    Your third form
    typedef struct person {
    char *name;
    char *surname;
    } person;
    loses the disadvantage of being an anonymous structure; you can now /also/
    define variables using the simple form of the structure
    (i.e. struct person *newperson;)
    You still /cannot/ use the typedef to define a recursive pointer
    (i.e. { person *nextperson; char *name; char *surname; } )
    but /can/ use the structure name instead
    (i.e. { struct person *nextperson; char *name; char *surname; } )


    Finally, your fourth form
    typedef struct _PERSON {
    char *name;
    char *surnmae;
    } person;
    you've already been told why _PERSON isn't a good name. Otherwise, this is
    no different from your third form. The only additional benefit might be
    that the difference in names would assist the programmer in keeping the
    usages straight.


    In programming, I tend to either your first form, or your third form,
    depending on the circumstances.

    HTH
    --
    Lew Pitcher
    "In Skills, We Trust"
    Lew Pitcher, Jun 10, 2013
    #1
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    839
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,680
    Smokey Grindel
    Dec 2, 2006
  3. Peter Bencsik
    Replies:
    2
    Views:
    805
  4. Andrew Thompson
    Replies:
    8
    Views:
    128
    Premshree Pillai
    Jun 7, 2005
  5. Replies:
    2
    Views:
    39
    Mark H Harris
    May 13, 2014
Loading...

Share This Page