case labels

R

Richard

Bartc said:
No different to:

if (i==f())
printf("f");
else if (i==g())
printf("g");

which is perfectly legal. It's a question of freedom of expression.

You need to think about the lines with or without break statements too
-this would then make the "else" incorrect. Do all the case functions
get execed or not? So no, in the absence of "case f()" being defined
nothing is obvious at all.
 
C

CBFalconer

Chris said:
Chuck, that's a completely missing-the-point answer; we /know/
that `f()` isn't a CIE.

You've been doing this sort of thing a /lot/ recently. I don't
know why, so my proposed solutions -- re-read before posting,
cut back on threads, coffee control, loud prog rock during
commute -- may not work for you.

I just looked. The quotes in my answer (repeated above) contained
the entire article to which I was replying. At this moment this
thread contains 65 entries, only some of which are available for
reading. Some are mislinked. Why should you expect me to review
an entire thread (which may be impossible, this is Usenet) before
making a reply? My usage makes 'impossible' more likely than for
most. I based the reply on what I read (and I concede it is
possible to read that somewhat differently).

Since I gather the originator knew he was suggesting an
illegitimate construct, then the obvious comment is that so
implementing would prevent generating a fixed transfer buffer with
one entry per case. This is normally the most efficient way of
implementing a switch. We might as well eliminate switch and just
rely on if/else if/else.
 
B

Bartc

Richard said:
You need to think about the lines with or without break statements too
-this would then make the "else" incorrect. Do all the case functions
get execed or not? So no, in the absence of "case f()" being defined
nothing is obvious at all.

You're right, 'break' is a bit of a nuisance, apart from spoiling the lines
of the somewhat cleaner code using switch.

But nevertheless it could be handled I think: with no breaks in the switch
example above, the if-version just becomes:

if (i==f()) {
printf("f");
goto L003;
}
if (i==g()) {
L003:
printf("g");
};

When case f() is false then the next case is tested, until a true case if
found. Then with break it will exit the whole block of cases, without break
it continues to the next case-body.
 
J

John J. Smith

Hi,

why general integer expressions are not allowed in case labels in
switch statements..????

If these expressions are not compile time constants, there's no guarantee
all case labels are unique. Consider..

extern int foo, bar;
int n;

/*...*/
n = 42;
switch(n)
{
case foo:
do_foo();
break;

case bar:
do_bar();
break;

default:
do_default();
break;
}

If foo and bar happen to have the same value (42), which branch should
be taken? The first matching one? All of them?

(Note that C disallows duplicate case labels probably for that reason)
 
R

Richard Heathfield

CBFalconer said:

Why should you expect me to review
an entire thread (which may be impossible, this is Usenet) before
making a reply?

Why should you expect to be able to make an *intelligent* contribution
without reading at least a large part of the articles in the chain from
the root article to the one you're replying to?

The person to whom you replied is Christian Bau. Not J Random Lamer, but a
person whose name you ought to recognise as someone who knows his C.
Therefore, whilst it is *possible* that he might make a simple mistake
such as using a function call as a label, it is surely more likely that he
is making a point, by example, about the language (as in fact he was, in
this case).

When one shoots from the hip, one tends to hit the wrong target.

<snip>
 
F

Flash Gordon

Richard Heathfield wrote, On 01/04/08 07:19:
CBFalconer said:




Why should you expect to be able to make an *intelligent* contribution
without reading at least a large part of the articles in the chain from
the root article to the one you're replying to?

In this case it only required reading the quoted material where the
question "why are constant integer expressions..." Given that question
it is perfectly reasonable that people would post "invalid" C code to
show what the problems would be if this rule did not apply.

When one shoots from the hip, one tends to hit the wrong target.

When one shoots from the hip at someone who is obviously not a valid
target there is no possibility of hitting the correct target. This,
obviously, is aimed at Chuck not RH.
 
D

David Thompson

Eric Sosman wrote:

strcmp. If the user wants something else, they can canonise
their switched string first. (And I'm imagining a `strhash`.)
Canonicalize. I don't want strings I have to worship.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

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,812
Messages
2,569,694
Members
45,478
Latest member
dontilydondon

Latest Threads

Top