Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > types, variable names and fields

Reply
Thread Tools

types, variable names and fields

 
 
SRoubtsov@gmail.com
Guest
Posts: n/a
 
      06-26-2008
Dear all,

Do you know whether ANSI C (or some other dialects) support the
following:
* a variable name coincides with a type name,
* a structure/union field name coincides with a type name
in the same file (.c + all relevant .h's)?

e.g.

typedef int m;

typedef struct a {
int i;
char m;
} b;

char *m
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      06-26-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Dear all,
>
> Do you know whether ANSI C (or some other dialects) support the
> following:
> * a variable name coincides with a type name,
> * a structure/union field name coincides with a type name
> in the same file (.c + all relevant .h's)?
>
> e.g.
>
> typedef int m;
>
> typedef struct a {
> int i;
> char m;
> } b;
>
> char *m

typedefs and variable identifiers (and struct/union tags) life in different
name spaces, so they can coexist if they have the same name.

Bye, Jojo


 
Reply With Quote
 
 
 
 
Harald van Dijk
Guest
Posts: n/a
 
      06-26-2008
On Thu, 26 Jun 2008 16:52:06 +0200, Joachim Schmitz wrote:
> (E-Mail Removed) wrote:
>> Dear all,
>>
>> Do you know whether ANSI C (or some other dialects) support the
>> following:
>> * a variable name coincides with a type name,
>> * a structure/union field name coincides with a type name
>> in the same file (.c + all relevant .h's)?
>>
>> e.g.
>>
>> typedef int m;
>>
>> typedef struct a {
>> int i;
>> char m;
>> } b;
>>
>> char *m

> typedefs and variable identifiers (and struct/union tags) life in
> different name spaces, so they can coexist if they have the same name.


No, assuming the same scope, no two typedefs, objects, or functions may
have identical names. Not in standard C, and not in any dialect that I'm
aware of. It's not possible to parse C if you can't determine what
declaration an identifier refers to. Given the above, if it were valid, it
would not be possible for a compiler to determine whether (m)+0 is a cast
of +0 to type m, or adds 0 to object m.

For structure/union tags ("a" in the above) and members, it's indeed
allowed to use the same name as for an existing object or typedef. There's
no problem with "char m;" in the above.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      06-26-2008
Harald van D?k wrote:
> On Thu, 26 Jun 2008 16:52:06 +0200, Joachim Schmitz wrote:
>> (E-Mail Removed) wrote:
>>> Dear all,
>>>
>>> Do you know whether ANSI C (or some other dialects) support the
>>> following:
>>> * a variable name coincides with a type name,
>>> * a structure/union field name coincides with a type name
>>> in the same file (.c + all relevant .h's)?
>>>
>>> e.g.
>>>
>>> typedef int m;
>>>
>>> typedef struct a {
>>> int i;
>>> char m;
>>> } b;
>>>
>>> char *m

>> typedefs and variable identifiers (and struct/union tags) life in
>> different name spaces, so they can coexist if they have the same
>> name.

>
> No, assuming the same scope, no two typedefs, objects, or functions
> may have identical names. Not in standard C, and not in any dialect
> that I'm aware of. It's not possible to parse C if you can't
> determine what declaration an identifier refers to. Given the above,
> if it were valid, it would not be possible for a compiler to
> determine whether (m)+0 is a cast of +0 to type m, or adds 0 to
> object m.
>
> For structure/union tags ("a" in the above) and members, it's indeed
> allowed to use the same name as for an existing object or typedef.
> There's no problem with "char m;" in the above.

So there were 2 questions:
>>> * a variable name coincides with a type name,

and the answer is no
>>> * a structure/union field name coincides with a type name

and the answer is yes.

Apparently I missed the first question and screwed up the answer for the
second...

To make up for that, here's what the Standard says:
6.2.1 Scopes of identifiers

1 An identifier can denote an object; a function; a tag or a member of a
structure, union, or

enumeration; a typedef name; a label name; a macro name; or a macro
parameter. The

same identifier can denote different entities at different points in the
program. A member

of an enumeration is called an enumeration constant. Macro names and macro

parameters are not considered further here, because prior to the semantic
phase of

program translation any occurrences of macro names in the source file are
replaced by the

preprocessing token sequences that constitute their macro definitions.

2 For each different entity that an identifier designates, the identifier is
visible (i.e., can be

used) only within a region of program text called its scope. Different
entities designated

by the same identifier either have different scopes, or are in different
name spaces. There

are four kinds of scopes: function, file, block, and function prototype. (A
function

prototype is a declaration of a function that declares the types of its
parameters.)

3 A label name is the only kind of identifier that has function scope. It
can be used (in a

goto statement) anywhere in the function in which it appears, and is
declared implicitly

by its syntactic appearance (followed by a : and a statement).

4 Every other identifier has scope determined by the placement of its
declaration (in a

declarator or type specifier). If the declarator or type specifier that
declares the identifier

appears outside of any block or list of parameters, the identifier has file
scope, which

terminates at the end of the translation unit. If the declarator or type
specifier that

declares the identifier appears inside a block or within the list of
parameter declarations in

a function definition, the identifier has block scope, which terminates at
the end of the

associated block. If the declarator or type specifier that declares the
identifier appears

within the list of parameter declarations in a function prototype (not part
of a function

definition), the identifier has function prototype scope, which terminates
at the end of the

function declarator. If an identifier designates two different entities in
the same name

space, the scopes might overlap. If so, the scope of one entity (the inner
scope) will be a

strict subset of the scope of the other entity (the outer scope). Within the
inner scope, the

identifier designates the entity declared in the inner scope; the entity
declared in the outer

scope is hidden (and not visible) within the inner scope.

5 Unless explicitly stated otherwise, where this International Standard uses
the term

''identifier'' to refer to some entity (as opposed to the syntactic
construct), it refers to the

entity in the relevant name space whose declaration is visible at the point
the identifier

occurs.

6 Two identifiers have the same scope if and only if their scopes terminate
at the same

point.

7 Structure, union, and enumeration tags have scope that begins just after
the appearance of

the tag in a type specifier that declares the tag. Each enumeration constant
has scope that

begins just after the appearance of its defining enumerator in an enumerator
list. Any

other identifier has scope that begins just after the completion of its
declarator.


 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-27-2008
(E-Mail Removed) wrote:

> Do you know whether ANSI C (or some other dialects) support the following:


It's ISO C these days, BTW.

> * a variable name coincides with a type name,


No, that's not allowed.

> * a structure/union field name coincides with a type name
> in the same file (.c + all relevant .h's)?


That is allowed. It's also allowed for the struct type itself to have
the same name as another type or object, because the struct keyword
disambiguates between them.

> typedef int m;
>
> typedef struct a {
> int i;
> char m;
> } b;
>
> char *m


The typedef and the char * clash. The char field does not clash with
either of those. And if you'd called it struct m, that would not have
clashed with any of the others, either.

Richard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic variable names or using a variable as another's name Peter Buckley Ruby 5 02-27-2009 06:05 PM
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
print struct fields and its member structs' fields recursively, generically call_me_anything C++ 4 09-30-2007 10:12 PM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM
logical puzzle: how to generate reasonable archive file names fromfile and directory names fBechmann Python 0 06-10-2004 07:13 AM



Advertisments