struct definition and scope

L

Laurent Deniau

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.
 
R

Richard Tobin

Laurent Deniau said:
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
 
J

Jack Klein

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:

struct A;

typedef void (T)(struct A*);

void f(void) {
struct A { int _; } a;
((T*)0)(&a);
}

./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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top