On Fri, 24 Aug 2007 04:14:15 -0700, Laurent Deniau
<La************@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.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html