scope of struct definitions

J

James Brown

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
 
M

Max TenEyck Woodbury

James said:
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'.
 
P

pete

James said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top