By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

case labels

P: n/a
Hi,

why general integer expressions are not allowed in case labels in
switch statements..????
Mar 28 '08
Share this Question
Share on Google+
55 Replies


P: n/a
Chris Dollin wrote:
CBFalconer wrote:
>"christian.bau" wrote:
>>aark...@gmail.com wrote:

why are constant integer expressions required in case labels of
the switch statement? what would be the impact of allowing
general integer expressions instead of constant integer
expressions? discuss both user convenience and implementation
aspects?

Consider

switch (i) {
case f (): printf ("f"); break;
case g (): printf ("g"); break;
}

Discuss under user convenience, implementation, and general
mental health aspects.

The function f() is not a constant integer expression. Illegal.

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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Apr 1 '08 #51

P: n/a
CBFalconer wrote:

<snip>
We might as well eliminate switch and just
rely on if/else if/else.
Simulating a Duff device with if/else is very ugly.

Apr 1 '08 #52

P: n/a
CBFalconer said:
Chris Dollin wrote:
>CBFalconer wrote:
>>"christian.bau" wrote:
aark...@gmail.com wrote:

why are constant integer expressions required in case labels of
the switch statement? what would be the impact of allowing
general integer expressions instead of constant integer
expressions? discuss both user convenience and implementation
aspects?

Consider

switch (i) {
case f (): printf ("f"); break;
case g (): printf ("g"); break;
}

Discuss under user convenience, implementation, and general
mental health aspects.

The function f() is not a constant integer expression. Illegal.

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.
<snip>
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>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Apr 1 '08 #53

P: n/a
Richard Heathfield wrote, On 01/04/08 07:19:
CBFalconer said:
>Chris Dollin wrote:
>>CBFalconer wrote:
"christian.bau" wrote:
aark...@gmail.com wrote:
>
>why are constant integer expressions required in case labels of
>the switch statement? what would be the impact of allowing
>general integer expressions instead of constant integer
>expressions? discuss both user convenience and implementation
>aspects?
Consider
>
switch (i) {
case f (): printf ("f"); break;
case g (): printf ("g"); break;
}
>
Discuss under user convenience, implementation, and general
mental health aspects.
The function f() is not a constant integer expression. Illegal.
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.

<snip>
>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?
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.

<snip good point>
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.
--
Flash Gordon
Apr 1 '08 #54

P: n/a
On Fri, 28 Mar 2008 14:00:05 +0000, Chris Dollin <ch**********@hp.com>
wrote:
Eric Sosman wrote:
If you switch on a `char*', should the matching
criterion be strcmp()? Or caseInsensitiveStrcmp()? Or
allWhiteSpaceCountsAsIdenticalStrcmp()?

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
Apr 7 '08 #55

P: n/a
David Thompson said:
On Fri, 28 Mar 2008 14:00:05 +0000, Chris Dollin <ch**********@hp.com>
wrote:
>Eric Sosman wrote:
If you switch on a `char*', should the matching
criterion be strcmp()? Or caseInsensitiveStrcmp()? Or
allWhiteSpaceCountsAsIdenticalStrcmp()?

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.
Presumably you are referring to St Ring?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Apr 7 '08 #56

55 Replies

This discussion thread is closed

Replies have been disabled for this discussion.