Re: What is better and why?

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

  1. BartC

    BartC Guest

    "paskali" <> wrote in message
    news:...
    > 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 don't know which is considered better; it's just a matter of style:

    I use variations of 2, 3 and 4. I like using a single identifier for the
    name (it hides the fact that it is a struct, and also makes it easy to
    change to something that isn't! And it makes type declarations more
    compact).

    I use a struct tag (as in 3 and 4) if a pointer to the type is one of the
    elements:

    typedef struct _person {
    char* name;
    char* surname;
    struct _person* next;
    } person;

    The underscore "_" is optional; struct tag names don't clash with typedef
    names (and for similar reasons, are unlikely to clash with reserved system
    names). But of course the name can be anything at all.

    However I use my own conventions for naming struct typedefs (so your
    examples might be named rperson or personrec). It could be argued that this
    isn't much different from using a 'struct' prefix anyway; I would say it's a
    bit more different!

    --
    Bartc
     
    BartC, Jun 10, 2013
    #1
    1. Advertising

  2. "BartC" <> writes:
    [...]
    > typedef struct _person {
    > char* name;
    > char* surname;
    > struct _person* next;
    > } person;
    >
    > The underscore "_" is optional; struct tag names don't clash with typedef
    > names (and for similar reasons, are unlikely to clash with reserved system
    > names). But of course the name can be anything at all.


    7.1.3p1: All identifiers that begin with an underscore are always
    reserved for use as identifiers with file scope in both the ordinary and
    tag name spaces.

    If the above declaration appears outside any function (which is the most
    likely place for it), it could conflict with an implementation-defined
    identifier "_person", either in a standard header or via compiler magic.

    It's probably true that an implementation is very unlikely to define
    "_person" for its own use -- but why settle for unlikelihood when you
    can have certainty just as easily?

    If you feel the need for a convention, you can always append an
    underscore:

    typedef struct person_ {
    ...
    } person;

    But as far as the language is concerned, no such "convention" is needed;
    there's no reason at all to use distinct identifiers for the tag name
    and typedef name.

    Making the identifiers distinct might have some advantage for
    non-language-defined tools (for example, it's easier to search for all
    occurrences of "person_" than for all instances of "person" not
    immediately following a "struct" keyword).

    > However I use my own conventions for naming struct typedefs (so your
    > examples might be named rperson or personrec). It could be argued that this
    > isn't much different from using a 'struct' prefix anyway; I would say it's a
    > bit more different!


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 10, 2013
    #2
    1. Advertising

  3. BartC

    James Kuyper Guest

    On 06/10/2013 02:00 PM, Keith Thompson wrote:
    > "BartC" <> writes:
    > [...]
    >> typedef struct _person {
    >> char* name;
    >> char* surname;
    >> struct _person* next;
    >> } person;
    >>
    >> The underscore "_" is optional; struct tag names don't clash with typedef
    >> names (and for similar reasons, are unlikely to clash with reserved system
    >> names). But of course the name can be anything at all.

    >
    > 7.1.3p1: All identifiers that begin with an underscore are always
    > reserved for use as identifiers with file scope in both the ordinary and
    > tag name spaces.


    "... each structure or union has a separate name space for its members
    ...." (6.2.3p1). They therefore do not conflict with names declared in
    either the ordinary or the tag name space.

    I think it's simpler to following a convention of avoiding the use of
    all identifiers that start with an underscore; but use as struct or
    union member names is safe.
     
    James Kuyper, Jun 10, 2013
    #3
  4. James Kuyper <> writes:
    > On 06/10/2013 02:00 PM, Keith Thompson wrote:
    >> "BartC" <> writes:
    >> [...]
    >>> typedef struct _person {
    >>> char* name;
    >>> char* surname;
    >>> struct _person* next;
    >>> } person;
    >>>
    >>> The underscore "_" is optional; struct tag names don't clash with typedef
    >>> names (and for similar reasons, are unlikely to clash with reserved system
    >>> names). But of course the name can be anything at all.

    >>
    >> 7.1.3p1: All identifiers that begin with an underscore are always
    >> reserved for use as identifiers with file scope in both the ordinary and
    >> tag name spaces.

    >
    > "... each structure or union has a separate name space for its members
    > ..." (6.2.3p1). They therefore do not conflict with names declared in
    > either the ordinary or the tag name space.
    >
    > I think it's simpler to following a convention of avoiding the use of
    > all identifiers that start with an underscore; but use as struct or
    > union member names is safe.


    The code used _person as a struct tag, not as a member name.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 10, 2013
    #4
  5. BartC

    James Kuyper Guest

    On 06/10/2013 06:00 PM, Keith Thompson wrote:
    > James Kuyper <> writes:
    >> On 06/10/2013 02:00 PM, Keith Thompson wrote:
    >>> "BartC" <> writes:
    >>> [...]
    >>>> typedef struct _person {
    >>>> char* name;
    >>>> char* surname;
    >>>> struct _person* next;
    >>>> } person;
    >>>>
    >>>> The underscore "_" is optional; struct tag names don't clash with typedef
    >>>> names (and for similar reasons, are unlikely to clash with reserved system
    >>>> names). But of course the name can be anything at all.
    >>>
    >>> 7.1.3p1: All identifiers that begin with an underscore are always
    >>> reserved for use as identifiers with file scope in both the ordinary and
    >>> tag name spaces.

    >>
    >> "... each structure or union has a separate name space for its members
    >> ..." (6.2.3p1). They therefore do not conflict with names declared in
    >> either the ordinary or the tag name space.
    >>
    >> I think it's simpler to following a convention of avoiding the use of
    >> all identifiers that start with an underscore; but use as struct or
    >> union member names is safe.

    >
    > The code used _person as a struct tag, not as a member name.


    I need to get some more sleep. Sorry for the noise.
     
    James Kuyper, Jun 10, 2013
    #5
    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:
    935
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,133
    Smokey Grindel
    Dec 2, 2006
  3. Peter Bencsik
    Replies:
    2
    Views:
    868
  4. Andrew Thompson
    Replies:
    8
    Views:
    161
    Premshree Pillai
    Jun 7, 2005
  5. Replies:
    2
    Views:
    63
    Mark H Harris
    May 13, 2014
Loading...

Share This Page