473,425 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

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.

Aug 24 '07 #1
2 4671
In article <11**********************@l22g2000prc.googlegroups .com>,
Laurent Deniau <La************@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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 24 '07 #2
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
Aug 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Razvan | last post by:
Hi ! Today I saw some code like this: struct foo { foo(foo* s2){ cout << "foo::foo"; }
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
20
by: Elliot Marks | last post by:
If a struct or its members are passed to a function, must it be declared globally? #include <stdio.h> struct mystruct{ int a; int b; }; int structfunc(struct mystruct foo);
8
by: Chris Barts | last post by:
Let's say we have a library with an opaque data type implemented as a struct in the library's C source file. The definition of the struct isn't duplicated in the header included by the programs...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
2
by: James Brown | last post by:
All, Could anyone explain why the following is an error: void foo( struct FOO { int x; } f1 ) { } int main()
17
by: djcredo | last post by:
Hey all, I want to return a pointer to a struct. Here is what I'lm trying to do: struct Position{ int x; int y; }; Position* GraphicTag::getRenderCentre(){
21
by: heavyz | last post by:
In C, if i want to declare a struct, there are 3 methods to do so: 1: struct dummy { ... }; 2: typedef struct { ... } dummy; 3: typedef struct _dummy { ... } dummy; Usually i use the first...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.