struct definition and scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laurent Deniau

    struct definition and scope

    I would like to know why the following small program does not compile
    (checked with gcc 4.1.2) and if the compiler behavior is correct:
    cat ./chk.c
    struct A;

    typedef void (T)(struct A*);

    void f(void) {
    struct A { int _; } a;
    ((T*)0)(&a);
    }
    gcc -std=c99 -pedantic -Wall -W -c ./chk.c
    ../chk.c: In function 'f':
    ../chk.c:8: warning: passing argument 1 of '0u' from incompatible
    pointer type

    If the declaration of a is moved just outside the definition of f,
    everything is right. It seems that the forward struct A declaration
    doesn't refer to the definition if it occurs inside f, but does if it
    occurs outside f (say same scope level)? References to C99 are
    welcome.

    The aim would be to have a 'generic' (global) function type
    declaration specialized case by case locally. Any clue to achieve
    this? My feeling is that it is not possible since it breaks the nested
    scopes encapsulation and C doesn't like it.

    a+, ld.

  • Richard Tobin

    #2
    Re: struct definition and scope

    In article <1187954055.498 771.286810@l22g 2000prc.googleg roups.com>,
    Laurent Deniau <Laurent.Deniau @gmail.comwrote :
    >struct A;
    >
    >typedef void (T)(struct A*);
    >
    >void f(void) {
    struct A { int _; } a;
    This declaration does not complete the incomplete type declared at
    file scope. It declares a new type.

    "A structure ... type of unknown content ... is an incomplete type.
    It is completed ... by declaring the same structure ... tag with its
    defining content later in the same scope." (C90, "Types")

    Your second declaration of A is not at the same scope.
    >The aim would be to have a 'generic' (global) function type
    >declaration specialized case by case locally.
    No, you can't specialise a declaration by completing it in different
    ways at different scopes.

    -- Richard
    --
    "Considerat ion shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.

    Comment

    • Jack Klein

      #3
      Re: struct definition and scope

      On Fri, 24 Aug 2007 04:14:15 -0700, Laurent Deniau
      <Laurent.Deniau @gmail.comwrote in comp.lang.c:
      I would like to know why the following small program does not compile
      (checked with gcc 4.1.2) and if the compiler behavior is correct:
      >
      cat ./chk.c
      struct A;
      >
      typedef void (T)(struct A*);
      >
      void f(void) {
      struct A { int _; } a;
      ((T*)0)(&a);
      }
      >
      gcc -std=c99 -pedantic -Wall -W -c ./chk.c
      ./chk.c: In function 'f':
      ./chk.c:8: warning: passing argument 1 of '0u' from incompatible
      pointer type
      >
      If the declaration of a is moved just outside the definition of f,
      everything is right. It seems that the forward struct A declaration
      doesn't refer to the definition if it occurs inside f, but does if it
      occurs outside f (say same scope level)? References to C99 are
      welcome.
      Yes, that is exactly right. The definition of a complete structure
      type in an inner scope complete eclipses the structure type (complete
      or incomplete) defined in the outer scope.

      This is no different than:

      /* file scope */
      struct s { int a, int b };

      void func1(struct s { double a, double b }; );

      void func2(void)
      {
      struct s { char *a, char *b } my_s =
      { "Hello, ", "World\n" };
      }

      Even though the file scope definition of struct s is complete in this
      example, it is overridden and replaced by the declarations in the two
      inner scopes.

      The declaration/definition in the argument section of the prototype
      for func1() has "function prototype scope" which terminates at the end
      of the prototyped (function declarator).

      The declaration/definition in the body of func2() has block scope, of
      course.

      See C99 6.4.2.1, especially paragraph 4.

      Note that this is always true, even when struct type definitions in
      inner and outer scopes are identical.

      See 6.2.7, mainly paragraph 1, to see how compatibility is defined for
      different struct, union, and enumeration types.
      The aim would be to have a 'generic' (global) function type
      declaration specialized case by case locally. Any clue to achieve
      this? My feeling is that it is not possible since it breaks the nested
      scopes encapsulation and C doesn't like it.
      I'm not sure what you mean by this, but you can't go about it this
      way. Perhaps you could do what you want using a macro.

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://c-faq.com/
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++

      Comment

      Working...