[comp.lang.c] Labels as values?

mikeg@c3.c3.lanl.gov (Michael P. Gerlek) (07/09/90)

While modifying someone else's (<-disclaimer) goto-and-label-ridden
code, I declared a variable and mistyped it later on -- and my typo
happened to be the name of an existing label.  Caused no end of
mysteries.

Simple case:

	char *ptr;

	label:
	...
	ptr = (char *)label;
	...

This code is accepted by both by VAX UNIX and Sun C compilers, but not
by gcc (no, I don't have version numbers handy, sorry).  I'm certainly
no expert, but I'm thinking gcc is right in complaining my use of
label in the assignment is invalid since label isn't declared as a
variable - if I do declare it, it works fine (even though the
identifier 'label' is used in two different contexts).

Neither K&R nor Harbison&Steele seem to have anything to say about
this.  What does the Standard say?  Is it legal to use a label as a
value-returning construct?  And if so, of what type should the label
be considered to be?


[  M.P.Gerlek (mikeg@lanl.gov)               ]
[  Los Alamos Nat'l Lab / Merrimack College  ]
[  Disclaimer: Yes, Mom, I'll play nice.     ]

diamond@tkou02.enet.dec.com (diamond@tkovoa) (07/17/90)

In article <MIKEG.90Jul8110356@c3.c3.lanl.gov> mikeg@c3.c3.lanl.gov (Michael P. Gerlek) writes:

>Simple case:
>	char *ptr;
>	label:
>	...
>	ptr = (char *)label;

According to the standard, an identifier is a primary (expression) if the
identifier designates an object or function.  A label does not designate
an object or function.  However, this is not in a constraints section or
syntax, so if a processor provides an extension for this, it is not required
to issue a diagnostic (unfortunately).

Label space is separate from ordinary identifier space.  So if you also
have an ordinary identifier (e.g. object, function, or typedef) also named
"label", then the processor must use the correct "label" in that expression
(i.e. not the label "label").  (Of course, if your ordinary identifier is a
typedef name, then the expression will be illegal for other reasons.)

The unwanted extension that was granted by several processors is even more
dubious, because the value assigned to "ptr" changes when you declare a
variable also named "label".  When the standard assigns a meaning to a
construct, a processor is not allowed to wave some extension and decide
that you get a different meaning.
-- 
Norman Diamond, Nihon DEC     diamond@tkou02.enet.dec.com
This is me speaking.  If you want to hear the company speak, you need DECtalk.