By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

scope of struct definitions

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.