Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > struct definition and scope

Reply
Thread Tools

struct definition and scope

 
 
Laurent Deniau
Guest
Posts: n/a
 
      08-24-2007
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.

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      08-24-2007
In article <(E-Mail Removed) .com>,
Laurent Deniau <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      08-24-2007
On Fri, 24 Aug 2007 04:14:15 -0700, Laurent Deniau
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments