3 usages of same name in different contexts !!

  • Thread starter karthikbalaguru
  • Start date
K

karthikbalaguru

Hi,

I have 2 different set of codes and have a
query based on it.

Set 1
--------
typedef struct Hello
{
int Hi,
Hello,
Hey;
}Hello;

main()
{
Hello h1;
h1.Hello =1;
printf("%d",h1.Hello);
}

In the above code,
The 3 usages of name Hello in the above code(Set 1) appears to
be identifiable / distinguishable by the compiler at any
single instance as the compiler does not show any error.

But, In the below code(Set 2), why does the compiler throw errors ?

Set 2
--------
typedef enum HelloType
{
Hi,
Hello,
Hey,
}Hello;

main()
{
Hello h1;
h1=1;
printf("%d",h1);
}

Any ideas ?

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

Hi,

I have 2 different set of codes and have a
query based on it.

Set 1
--------
typedef struct Hello
{
        int Hi,
        Hello,
        Hey;

}Hello;

main()
{
        Hello h1;
        h1.Hello =1;
        printf("%d",h1.Hello);

}

In the above code,
The 3 usages of name Hello in the above code(Set 1) appears to
be identifiable / distinguishable by the compiler at any
single instance as the compiler does not show any error.

But, In the below code(Set 2), why does the compiler throw errors ?

Set 2
--------
typedef enum HelloType
{
        Hi,
        Hello,
        Hey,

}Hello;

main()
{
        Hello h1;
        h1=1;
        printf("%d",h1);

}

Any ideas ?

I have #include<stdio.h> at the top of both the
set of codes. Also,
i have changed the 'main()' as 'int main(void)' and
do 'return 0' in the defintion of main of both the
set of codes.

With the above changes also, i get errors for the Set 2 code
and no errors for Set 1 code.

Karthik Balaguru
 
K

Keith Thompson

karthikbalaguru said:
I have 2 different set of codes and have a
query based on it.

Set 1
--------
typedef struct Hello
{
int Hi,
Hello,
Hey;
}Hello;

main()
{
Hello h1;
h1.Hello =1;
printf("%d",h1.Hello);
}

In the above code,
The 3 usages of name Hello in the above code(Set 1) appears to
be identifiable / distinguishable by the compiler at any
single instance as the compiler does not show any error.

But, In the below code(Set 2), why does the compiler throw errors ?

Set 2
--------
typedef enum HelloType
{
Hi,
Hello,
Hey,
}Hello;

main()
{
Hello h1;
h1=1;
printf("%d",h1);
}

Any ideas ?

I have #include<stdio.h> at the top of both the
set of codes. Also,
i have changed the 'main()' as 'int main(void)' and
do 'return 0' in the defintion of main of both the
set of codes.

It would be helpful if you'd remember to do this before posting.

In the first program, the struct tag is meaningful only when it
immediately follows the "struct" keyword, and the member name is
meaningful only when it immediately follows <expr>. where <expr> is an
expression of your structure type. There's always enough context to
distinguish which "Hello" is being used.

In your second program, an enumerator declares a constant of type int.
It's not directly tied to the enumerated type. To refer to "Hey", you
just write "Hey", not "Hello.Hey".

The best thing to do is just to avoid writing code like that in the
first place.
 
D

Daniel Pirch

However, in the second example you cited, it isn't clear to me why this
should be disallowed. Types and integer constants seem to occupy a
different space. Unclear why the compiler is designed in this way.

If enumeration constants and types were in different name spaces, what
should the compiler do with the following program:


#include <stdio.h>

enum { hello = 10 };
typedef int hello;

int main(void)
{
printf("%d\n", (hello)-3);
return 0;
}


Should the output be 7 or -3?
 
P

Phil Carmody

karthikbalaguru wrote:
[...]
typedef struct Hello
{
int Hi,
Hello,
Hey;
}Hello;

main()
{
Hello h1;
h1.Hello =1;
printf("%d",h1.Hello);
}

In the above code,
The 3 usages of name Hello in the above code(Set 1) appears to
be identifiable / distinguishable by the compiler at any
single instance as the compiler does not show any error.

You can a few more as well:

Hello:

Yup, can't be confused with any other use.
#define Hello Hello

That's not a name seen by the compiler, if one restricts that
term to not include the pre-processor.

That's not a name, ever.
/* Hello */

Nor is that.
#include "Hello"

Nor that.
I'm sure I left more out.

From the bag of non-names, plenty.

#error Hello
#pragma Hello
....

Phil
 
K

karthikbalaguru

If enumeration constants and types were in different name spaces, what
should the compiler do with the following program:

#include <stdio.h>

enum { hello = 10 };
typedef int hello;

int main(void)
{
printf("%d\n", (hello)-3);
return 0;

}

Should the output be 7 or -3?

Good example !!

Karthik Balaguru
 
B

Barry Schwarz

Hi,

I have 2 different set of codes and have a
query based on it.

If you are going to keep experimenting around the edges, you should
get a copy of n1256 and read it first to discover what the rules are.
This topic is covered in 6.2.3. Getting the official answer is much
easier than wading through the newsgroup looking for a kernel.
 
K

karthikbalaguru

If you are going to keep experimenting around the edges, you should
get a copy of n1256  and read it first to discover what the rules are.
This topic is covered in 6.2.3.  Getting the official answer is much
easier than wading through the newsgroup looking for a kernel.

The section 6.2.3 was informative.
I also came across the below lines in "The ANSI C Programming" by
Kernighan &
Ritchie.
"Enumerator names in the same scope must all be distinct from each
other and
from ordinary variable names, but the values need not be distinct. "

Karthik Balaguru
 
K

karthikbalaguru

The section 6.2.3 was informative.
I also came across the below lines in "The ANSI C Programming" by
Kernighan &
Ritchie.
"Enumerator names in the same scope must all be distinct from each
other and
from ordinary variable names, but the values need not be distinct. "

While walking through the K&R for another issue,
I got the below lines from K&R .

Structure
----------
"
A structure member or tag and an ordinary (i.e., non-member)
variable can have the same name without conflict, since they
can always be distinguished by context. Furthermore, the same
member names may occur in different structures, although as a
matter of style one would normally use the same names only for
closely related objects. "

Enumerator
-----------
"Enumerator names in the same scope must all be distinct from
each other and from ordinary variable names, but the values
need not be distinct. "

Karthik Balaguru
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top