struct definition and scope

Discussion in 'C Programming' started by Laurent Deniau, Aug 24, 2007.

  1. 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.
    Laurent Deniau, Aug 24, 2007
    #1
    1. Advertising

  2. In article <>,
    Laurent Deniau <> wrote:

    >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
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Aug 24, 2007
    #2
    1. Advertising

  3. Laurent Deniau

    Jack Klein Guest

    On Fri, 24 Aug 2007 04:14:15 -0700, Laurent Deniau
    <> wrote 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.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Aug 24, 2007
    #3
    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. Jianli Shen
    Replies:
    1
    Views:
    568
    Victor Bazarov
    Mar 13, 2005
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,358
    Chris Fogelklou
    Apr 20, 2004
  3. Ark
    Replies:
    1
    Views:
    406
    Chris Torek
    Aug 7, 2004
  4. Jon Slaughter
    Replies:
    4
    Views:
    433
    Jon Slaughter
    Oct 26, 2005
  5. Pierre Yves
    Replies:
    2
    Views:
    474
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page