types, variable names and fields

Discussion in 'C Programming' started by SRoubtsov@gmail.com, Jun 26, 2008.

  1. Guest

    Dear all,

    Do you know whether ANSI C (or some other dialects) support the
    following:
    * a variable name coincides with a type name,
    * a structure/union field name coincides with a type name
    in the same file (.c + all relevant .h's)?

    e.g.

    typedef int m;

    typedef struct a {
    int i;
    char m;
    } b;

    char *m
    , Jun 26, 2008
    #1
    1. Advertising

  2. wrote:
    > Dear all,
    >
    > Do you know whether ANSI C (or some other dialects) support the
    > following:
    > * a variable name coincides with a type name,
    > * a structure/union field name coincides with a type name
    > in the same file (.c + all relevant .h's)?
    >
    > e.g.
    >
    > typedef int m;
    >
    > typedef struct a {
    > int i;
    > char m;
    > } b;
    >
    > char *m

    typedefs and variable identifiers (and struct/union tags) life in different
    name spaces, so they can coexist if they have the same name.

    Bye, Jojo
    Joachim Schmitz, Jun 26, 2008
    #2
    1. Advertising

  3. On Thu, 26 Jun 2008 16:52:06 +0200, Joachim Schmitz wrote:
    > wrote:
    >> Dear all,
    >>
    >> Do you know whether ANSI C (or some other dialects) support the
    >> following:
    >> * a variable name coincides with a type name,
    >> * a structure/union field name coincides with a type name
    >> in the same file (.c + all relevant .h's)?
    >>
    >> e.g.
    >>
    >> typedef int m;
    >>
    >> typedef struct a {
    >> int i;
    >> char m;
    >> } b;
    >>
    >> char *m

    > typedefs and variable identifiers (and struct/union tags) life in
    > different name spaces, so they can coexist if they have the same name.


    No, assuming the same scope, no two typedefs, objects, or functions may
    have identical names. Not in standard C, and not in any dialect that I'm
    aware of. It's not possible to parse C if you can't determine what
    declaration an identifier refers to. Given the above, if it were valid, it
    would not be possible for a compiler to determine whether (m)+0 is a cast
    of +0 to type m, or adds 0 to object m.

    For structure/union tags ("a" in the above) and members, it's indeed
    allowed to use the same name as for an existing object or typedef. There's
    no problem with "char m;" in the above.
    Harald van Dijk, Jun 26, 2008
    #3
  4. Harald van D?k wrote:
    > On Thu, 26 Jun 2008 16:52:06 +0200, Joachim Schmitz wrote:
    >> wrote:
    >>> Dear all,
    >>>
    >>> Do you know whether ANSI C (or some other dialects) support the
    >>> following:
    >>> * a variable name coincides with a type name,
    >>> * a structure/union field name coincides with a type name
    >>> in the same file (.c + all relevant .h's)?
    >>>
    >>> e.g.
    >>>
    >>> typedef int m;
    >>>
    >>> typedef struct a {
    >>> int i;
    >>> char m;
    >>> } b;
    >>>
    >>> char *m

    >> typedefs and variable identifiers (and struct/union tags) life in
    >> different name spaces, so they can coexist if they have the same
    >> name.

    >
    > No, assuming the same scope, no two typedefs, objects, or functions
    > may have identical names. Not in standard C, and not in any dialect
    > that I'm aware of. It's not possible to parse C if you can't
    > determine what declaration an identifier refers to. Given the above,
    > if it were valid, it would not be possible for a compiler to
    > determine whether (m)+0 is a cast of +0 to type m, or adds 0 to
    > object m.
    >
    > For structure/union tags ("a" in the above) and members, it's indeed
    > allowed to use the same name as for an existing object or typedef.
    > There's no problem with "char m;" in the above.

    So there were 2 questions:
    >>> * a variable name coincides with a type name,

    and the answer is no
    >>> * a structure/union field name coincides with a type name

    and the answer is yes.

    Apparently I missed the first question and screwed up the answer for the
    second...

    To make up for that, here's what the Standard says:
    6.2.1 Scopes of identifiers

    1 An identifier can denote an object; a function; a tag or a member of a
    structure, union, or

    enumeration; a typedef name; a label name; a macro name; or a macro
    parameter. The

    same identifier can denote different entities at different points in the
    program. A member

    of an enumeration is called an enumeration constant. Macro names and macro

    parameters are not considered further here, because prior to the semantic
    phase of

    program translation any occurrences of macro names in the source file are
    replaced by the

    preprocessing token sequences that constitute their macro definitions.

    2 For each different entity that an identifier designates, the identifier is
    visible (i.e., can be

    used) only within a region of program text called its scope. Different
    entities designated

    by the same identifier either have different scopes, or are in different
    name spaces. There

    are four kinds of scopes: function, file, block, and function prototype. (A
    function

    prototype is a declaration of a function that declares the types of its
    parameters.)

    3 A label name is the only kind of identifier that has function scope. It
    can be used (in a

    goto statement) anywhere in the function in which it appears, and is
    declared implicitly

    by its syntactic appearance (followed by a : and a statement).

    4 Every other identifier has scope determined by the placement of its
    declaration (in a

    declarator or type specifier). If the declarator or type specifier that
    declares the identifier

    appears outside of any block or list of parameters, the identifier has file
    scope, which

    terminates at the end of the translation unit. If the declarator or type
    specifier that

    declares the identifier appears inside a block or within the list of
    parameter declarations in

    a function definition, the identifier has block scope, which terminates at
    the end of the

    associated block. If the declarator or type specifier that declares the
    identifier appears

    within the list of parameter declarations in a function prototype (not part
    of a function

    definition), the identifier has function prototype scope, which terminates
    at the end of the

    function declarator. If an identifier designates two different entities in
    the same name

    space, the scopes might overlap. If so, the scope of one entity (the inner
    scope) will be a

    strict subset of the scope of the other entity (the outer scope). Within the
    inner scope, the

    identifier designates the entity declared in the inner scope; the entity
    declared in the outer

    scope is hidden (and not visible) within the inner scope.

    5 Unless explicitly stated otherwise, where this International Standard uses
    the term

    ''identifier'' to refer to some entity (as opposed to the syntactic
    construct), it refers to the

    entity in the relevant name space whose declaration is visible at the point
    the identifier

    occurs.

    6 Two identifiers have the same scope if and only if their scopes terminate
    at the same

    point.

    7 Structure, union, and enumeration tags have scope that begins just after
    the appearance of

    the tag in a type specifier that declares the tag. Each enumeration constant
    has scope that

    begins just after the appearance of its defining enumerator in an enumerator
    list. Any

    other identifier has scope that begins just after the completion of its
    declarator.
    Joachim Schmitz, Jun 26, 2008
    #4
  5. Richard Bos Guest

    wrote:

    > Do you know whether ANSI C (or some other dialects) support the following:


    It's ISO C these days, BTW.

    > * a variable name coincides with a type name,


    No, that's not allowed.

    > * a structure/union field name coincides with a type name
    > in the same file (.c + all relevant .h's)?


    That is allowed. It's also allowed for the struct type itself to have
    the same name as another type or object, because the struct keyword
    disambiguates between them.

    > typedef int m;
    >
    > typedef struct a {
    > int i;
    > char m;
    > } b;
    >
    > char *m


    The typedef and the char * clash. The char field does not clash with
    either of those. And if you'd called it struct m, that would not have
    clashed with any of the others, either.

    Richard
    Richard Bos, Jun 27, 2008
    #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. fBechmann
    Replies:
    0
    Views:
    390
    fBechmann
    Jun 10, 2004
  2. wanwan
    Replies:
    3
    Views:
    415
    Alex Martelli
    Oct 14, 2005
  3. Chris McDonald

    bit-fields and permitted types (and [OT] gcc)

    Chris McDonald, Apr 18, 2005, in forum: C Programming
    Replies:
    1
    Views:
    594
    Eric Sosman
    Apr 18, 2005
  4. News123
    Replies:
    2
    Views:
    457
    John Machin
    Nov 26, 2008
  5. Peter Buckley
    Replies:
    5
    Views:
    194
    matt neuburg
    Feb 27, 2009
Loading...

Share This Page