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

types, variable names and fields

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
Jun 27 '08 #1
3 2008
SR*******@gmail.com 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
Jun 27 '08 #2
On Thu, 26 Jun 2008 16:52:06 +0200, Joachim Schmitz wrote:
SR*******@gmail.com 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.
Jun 27 '08 #3
Harald van D?k wrote:
On Thu, 26 Jun 2008 16:52:06 +0200, Joachim Schmitz wrote:
>SR*******@gmail.com 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.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
2
by: Gary | last post by:
My HTML reads a file and writes out field names to include a counter so I have fields like <input type=text name=surname$counter><input type=checkbox name=update$counter> When they click update...
15
by: pranab_bajpai | last post by:
So I want to define a method that takes a "boolean" in a module, eg. def getDBName(l2): .... Now, in Python variables are bound to types when used, right? Eg. x = 10 # makes it an INT...
7
by: PL | last post by:
I want to dynamicaly create variable and assigned it a particular name, for exemple: foreach (FileInfo FI in ArrayOfFiles) { string FI.Name = "AValueWhoDoesntMatter" } Of course, this...
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
2
by: Avi Laviad | last post by:
hi, sorry for the dumb question but how do i code a bit variable in c? i mean - shoudlnt the synax need to be: bit j; j = 1; correct me if im wrong (and i suppose i am). Avi.
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
4
by: Fester Bestertester | last post by:
Greetings, I'm still using MS Access 2K3 and DAO 3.6 objects (I know, I know, I'm a dinosaur)... I'm traversing through the fields of a table to retrieve their data types, thus, Dim...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.