473,772 Members | 3,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4705
In article <11************ **********@l22g 2000prc.googleg roups.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
--
"Considerat ion 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.l earn.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
2483
by: Razvan | last post by:
Hi ! Today I saw some code like this: struct foo { foo(foo* s2){ cout << "foo::foo"; }
15
9080
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 the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
20
12420
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
2778
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 that make use of this library. Is it legal for the header and the programs that include it to declare pointers to this struct for which no definition is in scope and to pass those pointers into functions declared in that header?
19
2643
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; } RtWidget;
6
2692
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 completed, for all declarations of that type, by ^^^ # declaring the same structure or union tag with its defining # content later in the same scope. ^^^^^ (6.2.5#23)
2
2294
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
3240
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
2009
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 method to declare my structs, but in many open source projects (such as libxml2), the third method is used. I am just curious about the difference.. Would somebody tell me which method is recommended, and why? Thanks in advance.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.