Article View: comp.std.c
Article #4852Re: integral types in switch expressions
From: Henry Spencer
Date: Fri, 01 Mar 1996 04:47
Date: Fri, 01 Mar 1996 04:47
19 lines
1588 bytes
1588 bytes
In article <4eb5r1$b04@news.tuwien.ac.at> schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz) writes: >I was surprised to learn that constant pointer expressions are >not allowed in case labels and that the expression in a switch statement >must be an integral type. (The reason for the exclusion of floating point >is obvious to me). I am interested in the technical reasons for this. Well, the practical reason is that C compilers have traditionally disallowed it and nobody made a convincing case for changing this. The technical reason for disallowing it is that doing even a vaguely clever implementation of switch requires knowing the values of the case labels at compile time. The values of pointer expressions are typically not known until link time; on systems using dynamic linking, they may not be known until run time. When the values are not known at compile time, it's hard to implement the switch as anything other than a series of ifs... and there is no point in writing that as a switch. -- Space will not be opened by always | Henry Spencer leaving it to another generation. --Bill Gaubatz | henry@zoo.toronto.edu
Technical Details
Message-ID:
<DnKnzv.8t6%spenford@zoo.toronto.edu>Path:
arc1-ca.pugleaf.net!howland.reston.ans.net!torn!sq!utzoo!spenford!henryReferences:
<4eb5r1$b04@news.tuwien.ac.at>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!howland.reston.ans.net!torn!sq!utzoo!spenford!henry From: Henry Spencer <henry@zoo.toronto.edu> Subject: Re: integral types in switch expressions References: <4eb5r1$b04@news.tuwien.ac.at> Sender: henry%spenford@zoo.toronto.edu (Henry Spencer) Organization: SP Systems, Toronto Date: Fri, 1 Mar 1996 04:47:54 GMT Message-ID: <DnKnzv.8t6%spenford@zoo.toronto.edu>