473,385 Members | 1,907 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,385 software developers and data experts.

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 2263
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
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...
5
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...
8
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...
1
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 };...
5
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...
3
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...
2
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) {...
1
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...
1
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: -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
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...

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.