Question 20.17

Is there a way to switch on strings?


Not directly. Sometimes, it's appropriate to use a separate function to map strings to integer codes, and then switch on those. Otherwise, of course, you can fall back on strcmp and a conventional if/else chain. See also questions 10.12, 20.18, and 20.29.

References: K&R1 Sec. 3.4 p. 55
K&R2 Sec. 3.4 p. 58
ANSI Sec. 3.6.4.2
ISO Sec. 6.6.4.2
H&S Sec. 8.7 p. 248


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback