473,765 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scope of struct definitions

All,

Could anyone explain why the following is an error:

void foo( struct FOO { int x; } f1 )
{

}

int main()
{
struct F f2;
return 0;
}
I get a 'f2' uses undefined struct 'FOO' error. So it looks like the scope
of type definitions declarated in a function prototype are essentially
'private' (i.e. contained) within the function and cannot be used outside.
Is this correct?

This is in contrast to nested structures defined within other structures:

i.e.

struct FOO
{
struct BAR
{
int y;
} b1;
};

struct BAR b2; /* this is ok */
In this last example, I can declare 'struct BAR b2' because I believe nested
structures use the name symbol namespace as 'global' tags. Really what is
confusing me, is that there is an apparent difference in 'namespace rules'
between structures declared in function prototypes, and nested structures
declared in container structures.

I guess my question is this: Does each function-object manage it's own
symbol namespace which is distinct from the global symbols? Or does this
namespace behaviour extend to any kind of nested block?

i.e.
void foo(struct FOO { int x; } f1)
{
{
struct FOO
{
int x;
} f1; /* seems legal, at least on the C-compiler I'm using */
}
}

Perhaps somebody could clarify what the namespace rules are for symbols, and
comment on if there are any differences between C89/90/99?

thanks,
James
Nov 3 '06 #1
2 2291
James Brown wrote:
>
Could anyone explain why the following is an error:

void foo( struct FOO { int x; } f1 )
{

}

int main()
{
struct F f2;
return 0;
}

I get a 'f2' uses undefined struct 'FOO' error. So it looks like the scope
of type definitions declarated in a function prototype are essentially
'private' (i.e. contained) within the function and cannot be used outside.
Is this correct?
Well not quite 'private'. There is not 'private'. As the FAQ explains,
the declaration for struct 'FOO' goes out-of-scope at the end of the
declaration of 'foo'. That is at the final '}' of 'foo'.
This is in contrast to nested structures defined within other structures:

i.e.

struct FOO
{
struct BAR
{
int y;
} b1;
};

struct BAR b2; /* this is ok */
In this last example, I can declare 'struct BAR b2' because I believe nested
structures use the name symbol namespace as 'global' tags. Really what is
confusing me, is that there is an apparent difference in 'namespace rules'
between structures declared in function prototypes, and nested structures
declared in container structures.
Not always global. It has the same scope as struct 'FOO'.
I guess my question is this: Does each function-object manage it's own
symbol namespace which is distinct from the global symbols? Or does this
namespace behaviour extend to any kind of nested block?

i.e.
void foo(struct FOO { int x; } f1)
{
{
struct FOO
{
int x;
} f1; /* seems legal, at least on the C-compiler I'm using */
}
}
It will compile but no one else can construct a struct FOO because the
definition is only available inside 'foo'.
Perhaps somebody could clarify what the namespace rules are for symbols, and
comment on if there are any differences between C89/90/99?
Nov 3 '06 #2
James Brown wrote:
>
All,

Could anyone explain why the following is an error:

void foo( struct FOO { int x; } f1 )
{

}

int main()
{
struct F f2;
struct FOO f2; /* ? */
return 0;
}

I get a 'f2' uses undefined struct 'FOO' error.
So it looks like the scope
of type definitions declarated in a function prototype are essentially
'private' (i.e. contained) within the function
and cannot be used outside.
Is this correct?
Yes.

N869
6.2.1 Scopes of identifiers
[#2]
There are four kinds of scopes:
function, file, block, and function prototype.

--
pete
Nov 4 '06 #3

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

Similar topics

3
1763
by: Alfonso Morra | last post by:
I have some code that I am porting over from C. It is full of static functions and global variables. I have got around this by wrapping most of the code in a singleton object. However, I am findng that in my method definitions, I am having to fully scope the names of any other functions I am calling. I do not understand this. The clas declaration code look some thing like this: class A: public Singleton<A> { public:
5
2095
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope function-prototype scope I think(might be wrong):
8
3378
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
1
2149
by: wenmang | last post by:
Hi, I encountered some legacy codes with multiple definitions for some symbols in term of enum(global naemspace pollution). How can I enforce the scope of an enum? e.g., enum MyEnum { OK }; enum YourEnum {
5
2036
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default arguments, and constructor ctor-initializers in that class (including such things in nested classes).
3
16556
by: psroga | last post by:
Can anyone look at this code and let me know why pthread_mutex_unlock and pthread_mutex_lock are giving me the "phtread_mutex_unlock" was not defined in this scope error. I am compiling the code on g++. # g++ -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man -- infodir=/usr/share/info --enable-shared --enable-threads=posix -- enable-checking=release --with-system-zlib...
2
4705
by: Laurent Deniau | last post by:
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);
1
1326
by: George | last post by:
Hi All, With gcc (3 or 4) the member template definition out of class scope below compiles fine. But with Sun's CC 5.9, it fails with: "main.cpp", line 7: Error: Could not find a match for Foo<T, Policy<struct>>::bar<Foo<T, Policy<struct>>::U>(U). This seems like a bug to me, has anyone else encountered this? Is there any workaround beside putting the definition inside the class
1
3760
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10007
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
9959
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
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
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
6649
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
5277
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
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.