Article View: comp.lang.c
Article #647Re: Taming The Switch-Statement
From: David Brown
Date: Wed, 21 Oct 2026 14:40
Date: Wed, 21 Oct 2026 14:40
358 lines
15998 bytes
15998 bytes
On 21/10/2026 13:29, bart wrote: > On 21/10/2026 10:20, David Brown wrote: >> On 20/10/2026 22:07, bart wrote: >>> On 20/10/2026 16:02, David Brown wrote: >>>> On 20/10/2026 16:11, bart wrote: > >>>>> * A further complication was to allow function pointers within that a >>>>> declaration list > >>>> To me, the big disadvantage of C's declaration syntax is that it >>>> lets you declare things of different (but related) types in the same >>>> declaration. >>> >>> Yes, but that doesn't bite so much because it is uncommon. >>> >> >> Unfortunately not - but it is uncommon to mix objects and function >> declarations, or objects of wildly different real types. > > That's what I meant. Ok. It's nice to agree sometimes! > >>> const int const * const p; >>> 1 2 3 >>> >>> const 1 applies to the basetype to its right. >>> const 2 applies to the basetype to its left. >> >> These are redundant - they do the same thing. (I'd have preferred if >> the ordering rules were strict - allowing only "const int" or "int >> const" - but at least people are usually consistent in their style.) >> >>> >>> (Both also can be in the middle with compound types like 'long long'!) >>> >>> const 3 is tricky: I think it applies to the * to its left, since it >>> says that the pointer value itself is const. But 'p' has type >>> 'pointer to', so it could apply to that directly. >> >> Yes - it is "p" that is const, not the "*". > > I would disagree. This type can also be written as: > > (const int const * const) > > There is no 'p' to apply to. The type describes a pointer to an integer. > The first two consts say, redundantly, that the integer value is const. > > The third must then apply to the pointer. Yes. But "the pointer" is the thing you are declaring. I don't think it is worth belabouring this. > >>> >>> What about this however: >>> >>> int * const * p; >>> >>> 1 2 3 >>> >>> I think here, const 2 applies to * 1, which is to it's left, and >>> can't apply to 'p' as that is the other side of the * 3. >> >> *p is of type "int * const" - thus you can use "*p" to access a >> pointer- to-int, but you can't use *p to change that pointer-to-int. > > But what exactly does this 'const' apply to? The type of "*p" - the thing that "p" points to. > > I'm suggesting any 'const' applies either to the base type (the one on > the left) or to a particular '*'. > > In that case the latter 'const's must apply right-to-left to the > preceding '*'. You can view it that way. There is a style known as "east const" that aims to put "const" on the right where possible (thus "int const", rather than "const int"), so that it always applies to the thing immediately to its left. I have never liked the style, but it is arguably more consistent. > >>> Fair to say that is a complete mess, and it is error prone. >> >> No, it is not at all fair to say that. It is fair to say that a >> different way of writing types might make it easier to understand - >> but that's a /long/ way from saying it is "a complete mess". And you >> can't call it "error prone" unless you know that people regularly use >> such declarations, and often have bugs in their code as a result of >> doing so incorrectly. I think that is an unrealistic claim. > > If there's any confusion about which const applies to which type > element, then mistakes can be made. Mistakes can always be made. And mistakes can always be found, and fixed. The questions are, are mistakes made often, are they made more often than they would be with a different syntax, are the mistakes left undiscovered, and are they consequential? I don't think so - so I don't see the syntax as "error prone". Again, I do not think the C declaration syntax is the nicest or clearest to be found, compared to some languages - but I do not think it is nearly as bad in practice as some people make out. > >> You can write this in C++ : >> >> // This bit is only needed once >> template <typename T> using ptr = T *; >> >> // p is a pointer to a const pointer to an int >> ptr<const ptr<int>> p; >> >> >> That's LTR syntax. Sure, it also has < > parentheses, but that's a >> minor point since the ordering of the components is what apparently >> matters so much. > > That it is LTR is now irrelevant because I find that incomprehensible > anyway. You've turned a linear chain a nested structure ... oh never > mind, I don't even want to go there with C++. Your LTR form /is/ a nested structure - it's just that with this C++ format (which I have never seen anyone use in practice), the nesting is explicit. >> >> Or you can write : >> >> typedef int * p_int; >> const p_int * p; >> >> Thus p is a pointer to a "const p_int", where a "p_int" is a pointer >> to an int. > > So, a common pattern with you is to use workarounds so that you > effectively emulate LTR type patterns. Because they are easier to work > with and understand. I'm offering you an alternative, because /you/ find the normal C syntax confusing. /I/ would not write that, because I don't find the C syntax here confusing or difficult to read. And mostly I would not write "int * const * p" because I have no idea when such a type might be useful. (If I knew, then maybe I would give it a specific name.) > > So you do appreciate how they facilitate things, even though they > introduce an extra level of indirection and also disseminate the type > information across the program. I appreciate that you are easily confused and have trouble with C syntax. I recommend you do one of two things - stop worrying about imaginary types with no meaning, or learn to use typedef so that they are not so scary for you. > > But you want to underplay how useful it would be to have natural LTR > type declarations within a syntax. > I don't see LTR syntax as making a /significant/ or fundamental difference. There will be a narrow range of complex types where the C syntax is getting hard to follow, but an LTR syntax is easier, but beyond that LTR is equally impenetrable. The issue for complex types is not the syntax, it is the complexity of the type - and you handle that by breaking things down into manageable sections. Let us put some numbers into this. (To be clear - these numbers are pulled out of the air, and "real" values will depend on the reader, the context, the details of the real code, real alternative syntax, etc.) Let us say that C syntax is comprehensible with up to 4 parts - where a "part" is a "const", a "*", "[n]" - in addition to the base type. With an LTR syntax, let us say it is comprehensible up to 5 parts. Then there is no doubt - the LTR syntax /is/ clearer. But does it actually matter, in practice? Most declarations require no more than 3 parts - with the great majority clustered at 0 or 1 parts. Who cares if it is difficult to read a type declaration you almost never need? And then if you have something really complicated - 6 parts or more - it is equally incomprehensible with LTR and C syntax, and should be handled by divide-and-conquer just like anything complicated in programming. This is why I can agree that the C syntax is not optimal, that an LTR syntax can be clearer, and yet it does not actually matter and the C syntax works fine in practice. >> These are different ways to write the same type, including one that is >> LTR, and the main hinder to understanding remains the same - what is >> the type for? > > If you create a type alias voluntarily (I don't mean structs where > either a tag or a typedef needs to be created) then yes the name should > impart something useful. > > But in my view this is often taken too far. Sometimes you have structs > where every member has a different user-type. When you look further, you > find they are all ints. That is just obfuscation. Every good thing can be taken too far - but there are lots of good reasons for having multiple typedefs that all coincidentally happen to boil down to "int". > > (I would accept enumeration types if the compiler can employ that info > to improve type safety, but that seems to be limited.) I agree that C enumeration types are not as "strong" as I'd like. Compiler static analysis can help in many ways, but it would be better (for me) if they were strongly typed and scoped. > >>> I've showed even simple types or trying to use 'const' get becomes >>> confusing. >> >> No, you haven't. > > OK. That is not surprising. > >>> It would make a HUGE difference: >> >> Your points below are all true, but IMHO all very minor, and do not >> add up to anything much. > > It would eliminate half the issues with C's type syntax. Probably CDECL > wouldn't need to exist. Lots of () disappear. It would make pointers-to- > arrays more viable. > > But that's nothing in your view. OK again. CDECL has never needed to exist. So few () are needed in declarations that it doesn't matter if they are there or not. Pointers to arrays work fine if you want to use them, but they are not often used in C - primarily because of the array-to-pointer decay in expressions, not because of the declaration syntax or the prefix dereferencing operator. > >> Your invented confusion. > > >> That doesn't matter in practice. > >> I don't see that as significant. >> >> I do agree that the decay of expressions using array types into >> pointer types is a source of error in C code - though I do not think >> it is as bad as you do. > > I didn't say anything about, and that wouldn't be affected. > Ok. > >> But I just don't see pointer-to-array expressions to be common enough >> for the difference you describe to be important. > > > If you're passing arrays to a function by-reference then the natural way > is to a have a pointer to the array. C can do that, but nobody ever does > so, probably because indexing is then so ugly. > > With my idea, (*A)[i] turns into A*[i], a bit less ugly. Predictions are of course speculative, but I could not see that making noticeable difference. C programmers rarely use pointers to arrays because they rarely give advantages over pointers to the first element of an array, the decay in expressions means it is easy to make mistakes, and you often need to make a pointer to the first element anyway. If array expressions did not decay to pointer expressions, and array parameters were passed as values, it would be a very different matter. > >>> (4) For pointers to structs, these are the patterns: >>> >>> prefix * With -> With postfix * >>> >>> (*P).m P->m P*.m >>> (**P).m (*P)->m P**.m >>> (***P).m (**P)->m P***.m >>> >>> Which column looks the tidiest? >> >> Far and away the most common - by orders of magnitude - are pointers >> to structs. "p->m" looks fine to me, and I prefer it to "p*.m". > > But where's the consistency? Sometimes "." is used for member selection, > and sometimes "->". And with two pointer levels, you need both! The consistency is that you almost always have a pointer to a struct, so you can consistently use p->m. There is no need to change that for consistency with a much rarer usage. And of course "." is used for member selection when you have a struct object, or when you have already dereferenced a pointer to a struct. >>> (6) Your 'int * a, b' example would become: >>> >>> int a *, b; >>> >>> Much easier, even with that space, to see that '*' does not belong to >>> the base type but is a modifier for that variable. >> >> It's still bad. I don't think finding a way to make it easier to >> write bad code is a good idea. > > It wouldn't stay bad for long! Because if the intention was to declare > two pointers, then it's easier to see that this is wrong. It is bad (according to my personal style preferences) if your intentionally declare an int and a pointer to an int in the same line. I am not talking about mistakes here - a mixup between an int and an int* will almost certainly result in a compilation failure and the code will quickly be fixed to give valid C code. C lets you write "int *a, b;" - it is legal C. There are lots of things you can write in C that I think are bad code, even though they may be intentional and work as intended. (And even though others will, naturally, have different opinions.) I would be against any change to C syntax that made it /easier/ for people to write such declarations - whether intentional or by mistake. > >> So basically, the suggestion would change some very minor things - >> mostly in subjective ways - and not actually do anything for aspects >> of the language where there is more of a consensus that C is sub- >> optimal. And for this, you would want a fundamental change to one of >> the most used operators in all C code! > > Obviously you can't change prefix * now. A postfix /alternative/, in > addition to prefix *, could have been introduced decades ago. So by the > time /you/ encountered C, you might have been using the latter. > I first used C on a very small scale in the late 1980's, so that was quite a few decades ago. I expect that a postfix dereferencing operator would have worked fine instead of a prefix one - it works fine in Pascal. I don't think it would have been "better" in any significant way. Two separate dereferencing operators with no difference except the prefix vs. postfix positioning would have been a very bad idea, no matter when it were introduced - it would have been a totally unnecessary complication, just adding confusion and inconsistency. But even if you were right here, it is no more realistic to go back 4 decades to add this new postfix dereferencing operator as it is to go back 5 decades and pick that one in the first place.
Technical Details
Message-ID:
<11bais3$1042$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11avmfb$55rt$1@dont-email.me> <11b0cvs$skc$1@reader2.panix.com> <11b0mfv$j5ll$1@dont-email.me> <11b3r71$6me$1@reader2.panix.com> <11b500f$216c4$1@dont-email.me> <11b6ekh$d2qm$1@paganini.bofh.team> <11b7sqn$32pr5$1@dont-email.me> <11b7vqd$2m8c9$6@dont-email.me> <11b8hln$3be6n$1@dont-email.me> <11ba045$3peq3$1@dont-email.me> <11ba7na$3sh37$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Wed, 21 Oct 2026 16:40:02 +0200 Organization: A noiseless patient Spider Lines: 358 Message-ID: <11bais3$1042$1@dont-email.me> References: <119qf6l$2rmg3$1@dont-email.me> <11avmfb$55rt$1@dont-email.me> <11b0cvs$skc$1@reader2.panix.com> <11b0mfv$j5ll$1@dont-email.me> <11b3r71$6me$1@reader2.panix.com> <11b500f$216c4$1@dont-email.me> <11b6ekh$d2qm$1@paganini.bofh.team> <11b7sqn$32pr5$1@dont-email.me> <11b7vqd$2m8c9$6@dont-email.me> <11b8hln$3be6n$1@dont-email.me> <11ba045$3peq3$1@dont-email.me> <11ba7na$3sh37$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 21 Oct 2026 14:40:04 +0000 (UTC) Injection-Info: dont-email.me; logging-data="32898"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18qfRL1a9aTQo5TVRLmMRj3EEdSGpigHDk="; posting-host="f78456a6491f23b3f53847b5a3287905" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:ErSNU41Aqin0yoOLyQXYCH9Zngk= sha256:KRqSawB7zKe60qRZvyhabu9TCOg01XSIKO3ZxVl6iRY= sha1:KNwmPQG/jWnOYuKsFKeHQyBsCMA= sha256:L1NyLUgK50A7l0ektiAXj9Ix0LghKJ1fSNdzccyt5nU= In-Reply-To: <11ba7na$3sh37$1@dont-email.me> Content-Language: en-GB, nb-NO