🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #639

Re: Taming The Switch-Statement

#639
From: bart
Date: Wed, 21 Oct 2026 11:29
247 lines
10580 bytes
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.

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

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

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 '*'.

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

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

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.

But you want to underplay how useful it would be to have natural LTR 
type declarations within a syntax.

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

(I would accept enumeration types if the compiler can employ that info 
to improve type safety, but that seems to be limited.)

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

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


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

(In my language, it was A^[i]. However, some years ago I relaxed the 
rules to allow such in-between derefs to be dropped - the compiler will 
reinsert them as needed.

So indexing looks like A[i], and there is the extra type safety of 
working with actual arrays, not pointers to things that may or may not 
be arrays.

But then I also have pass-by-reference which achieves the same thing.)

>> (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!

(Again in my language, all those * (or ^) in the right column can be 
dropped. Some loss of transparency, but much, much cleaner code.

But TBF, this could also be done in C without needing postfix *.)


>> (5) With prefix *, a pointer type looks like 'T*'; notice the * is on
>>      the right, not left! With postfix *, it would all be consistent. You
>>      also wouldn't have 'T*A'; that would be an error (T* only happens
>>      when there is no name)
> 
> The C declaration syntax is "declaration follows use".  If "*" were a 
> postfix operator, then the declaration of a pointer-to-int would become:
> 
>      int p*;
> 
> while the type of p would be "int *".  So you would still have an 
> inconsistency there, unless you also changed the way C has its 
> declarations.

The point is that currently you have 'int*' for the type, '*p' to 
declare p, and '*p' in expressions. The '*' switches sides for the bare 
type!

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

> 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.
Technical Details
Message-ID: <11ba7na$3sh37$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-mail
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>

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: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Wed, 21 Oct 2026 12:29:45 +0100
Organization: A noiseless patient Spider
Lines: 247
Message-ID: <11ba7na$3sh37$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 21 Oct 2026 11:29:47 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="4080743"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19TW45OOSn8aGUB+8SqeVZ8"; posting-host="c65e78a22dc81d6f5c20317bcf66a407"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xkV2p+Eiga8OyVC/HHfac7DfXJk= sha256:xbrAaf9Pwo3IdxGMFDxEV5TnXEC7Nm2Ci7IbRAlYZ3c=
	sha1:oSnjgbZzte4hOyGop5uUoev+3eE= sha256:fP9/X684clrDMiufRVAiSe3/nPSKQviXxPcuB6dk4Rg=
In-Reply-To: <11ba045$3peq3$1@dont-email.me>
Content-Language: en-GB