name space and typedef

S

S.Tobias

typedef int myint;

struct s
{
myint myint; /* 1 */
} s;

int main()
{
myint myint; /* 2 */
s.myint = 5; /* 1 */
myint = 1; /* 2 */
return myint; /*kill warning*/
}

What is interesting to me is case 2. "Typedef myint" and "object myint"
identifiers belong to the same name space, so I think there should
be a conflict (there's no conflict in case 1 - members have a name
space of their own). However, two compilers accepted the above code.
 
C

Chris Torek

typedef int myint;

struct s
{
myint myint; /* 1 */
} s;

int main()
{
myint myint; /* 2 */
s.myint = 5; /* 1 */
myint = 1; /* 2 */
return myint; /*kill warning*/
}

What is interesting to me is case 2. "Typedef myint" and "object myint"
identifiers belong to the same name space, so I think there should
be a conflict (there's no conflict in case 1 - members have a name
space of their own). However, two compilers accepted the above code.

They are indeed in the same name-space; but the declaration of the
block-scope variable in "2" is in an inner scope, which hides the
outer-scope name. This is therefore no different than:

int i;
int f(void) {
int i;
...
}

or, perhaps even closer but C99-specific:

int i;
int f(void) {
i = 3; /* sets the file-scope i */
int i = 2; /* creates a new i */
i = 1; /* sets the block-scope i */
...
}

(Actually *implementing* "myint myint;" in C compilers tends to be
pretty tricky, while implementing the scoped "i"s tends to be easy,
but this is an artifact of typical C implementations.)
 

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
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top