🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #610

Re: Taming The Switch-Statement

#610
From: David Brown
Date: Tue, 20 Oct 2026 15:02
198 lines
8928 bytes
On 20/10/2026 16:11, bart wrote:

> The other was C. There, several choices were made:
> 
> * To start with that Fortran style except that [] are used
> 
> * To decree that that 'A[20]' syntax is supposed to match indexing of
>    A as it might appear in an expression
> 
> * To introduce "*" as a prefix dereference operator in expressions ...
> 
> * ... which then means that a prefix "*" appears in the declaration too
> 
> * A further complication was to allow function pointers within that a
>    declaration list

I don't see a problem with that.  If you need to declare a function 
pointer, you need to declare it in 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.  I don't have a problem with :

	int (*my_func_pointer)(int);
	int my_array[20];

but I /do/ have a problem with :

	int (*my_func_pointer)(int), my_array[20];

or, more common and therefore worse :

	int * a, b;

The way to deal with this is, of course, simply not writing such mixed 
declarations.  But some people /do/ - living with other peoples' code is 
the bane of all programmers.

I am confident that there could have been a nicer syntax, and function 
pointer (or function type) syntax is particularly ugly.  But it works 
well enough in practice that I cannot see it as a problem or even 
inconvenience.

> 
> * Yet one more which came later was to add 'const', where placement now
>    gets confusing

Some people do find it hard to understand the difference between :

	const int * p;
	int * const q;
	const int * const r;

I note that this is despite the clear left-to-right nature of "const" here.

Many newbie mistakes with const will be caught by the compiler, and once 
you get used to it, you should be fine.

> 
> But to stay with pointers, take this expression:
> 
>     *A[i]
> 
> C precedence rules mean this is parsed as *(A[i]), which means that a 
> type written as 'T *A[N]' must be an array of pointers to T (I had to 
> think for a few seconds!).

If you instead forget that you can declare more than one thing in one 
declaration, you can write :

	T* A[i];

Then it is quite clear.

> 
> But suppose you wanted a pointer to array of T? The expression for that 
> would be:
> 
>      (*A)[i]
> 
> And so the declaration has to be 'T (*A)[N]' too.


Yes.  Declaration syntax follows usage.  (If you don't like the operator 
precedence, or the fact that dereferencing is pre-fix in C, unlike, say, 
Pascal, then that's an orthogonal dislike.)

> 
> So it is unfortunate that parentheses used to control precedence in an 
> expression now also have to appear in type declarations (where they are 
> often next to () used for parameter lists, for further confusion).

You know how to use typedefs to make code clearer.

Complicated types are a minority.  Most object declarations are either 
things, arrays of things, or pointers to things - not combinations of 
these.  And most function declarations are just function declarations, 
not arrays of function pointers or other complex types.

> 
> ==============================================
> 
> So, what would have happened with a postfix deref op? (Ignore whether 
> '*' would still be a good choice.) My first example would be this:
> 
>     A*[i]
> 
> Here, it would mean that A is dereferenced, then indexed; A must then be 
> a pointer to an array of T.
> 
> But suppose we wanted the original array of pointers to T, where you 
> index first then dereference. That is written like this:
> 
>     A[i]*
> 
> You simply swap the order of [] and *.
> 
> So, PARENTHESES HAVE BEEN ELIMINATED for purpose of disambiguating 
> array/pointer and pointer/array, both within expressions and within 
> type-declarations.

Agreed.

But does that make a significant difference?  I don't think so.  I'd 
have to see a lot more real-life examples to get a feeling for whether 
postfix dereference would be better or worse than prefix - saving the 
occasional parentheses is not sufficient.

> 
> How about that? Further, most of the type-spec is now LTR; only the 
> base-type has to stay on the left. Access terms in expressions will be 
> fully LTR.
> 
>> IMO C suffers because typedef was added late in the game and
>> function prototypes are much later.  Prototypes need types and
>> to get it one needs "hole" in variable declaration.  Where the
>> hole should be is specified by the C syntax, but placement of
>> hole is probably least clear aspect of declarations.
> 
> I thought that 'hole' was where 'int A[10]' mean one thing in normal 
> declaration but another within a parameter list.

That's a different thing entirely.  (And while I don't find it confusing 
that array and pointer parameters mean the same thing in C, I do see it 
as a wasted opportunity for better static checking and optimisation of 
code.)

> 
>> IMO C declaration syntax was a mistake, but as real life shows
>> it was not a fatal mistake, people can adapt.
> 
> A mistake that could so easily have been avoid by choosing a different 
> precedence as shown above. Did none of the thousand people working at 
> Bell Labs (according to a comment in DC's follow-ip) spot this and say 
> anything?

I suspect the folks at Bell Labs were too busy writing real code to be 
bothered by how confused you would be with artificially complex types.

Life is full of little decisions that, with hindsight, might have been 
sub-optimal choices but are nonetheless good enough in practice.  C's 
declaration syntax is one of those decisions.  It's worth learning from 
that and picking a clearer syntax for new languages, but it is not worth 
obsessing over its use in existing languages.


> 
> Had no-one even glanced at a Pascal spec, if there was no working copy? 
> It would have transformed C type syntax.
> 
> (ACTUALLY IT IS NOT TOO LATE. A secondary postfix deref op could have 
> been added. This could have been done 50 years ago, with the original 
> eventually deprecated.

It /is/ too late - far, far too late.  It would be a terrible idea to 
add a new postfix dereference operator to give two different operators 
with no benefit other than very occasionally reducing the need for 
parentheses.  If "*p" really bothers you, write "p[0]" instead (in 
expressions, not type declarations).

> 
> But this is a common theme of mine whether I perceive a problem that 
> people grudgingly admit might not be idea. But nothing was ever done to 
> fix it even after decades.)
> 

What is the common theme?  You pick something that lots of people 
consider as "it's maybe uglier than it need be, but it works fine in 
practice" and pretend that you alone discovered the issue?  Then you go 
completely haywire about how terrible and unbelievable it is, call the C 
designers idiots and claim to be a genius because your irrelevant 
language does something different?  Then comes the paranoia, and claims 
that no one will "admit there is a problem", and the absurd insistence 
that we all "do something about it".  Eventually the thread dies away, 
until you bring up the same shit half a year later.  For years, and 
years, and years.  Is that the common theme you are talking about?

Technical Details
Message-ID: <11b7vqd$2m8c9$6@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>

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: Tue, 20 Oct 2026 17:02:37 +0200
Organization: A noiseless patient Spider
Lines: 198
Message-ID: <11b7vqd$2m8c9$6@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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 20 Oct 2026 15:02:38 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2826633"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+lIzsyLvpZVa3rKbfMUEtp7cHq7yiinio="; posting-host="6609bebd5817c7daf1cbce2eb44e7728"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:UREU0xdk1oH1JBxfHmD3dXsJ878= sha256:MUE8L/t1jll6MECE33RRAfxUv6z4888ZF9VJ+3uW1I4=
	sha1:5Q+3pxehOwt9QWKCdfbHkuBK9H8= sha256:HVbBY4MAQmSaXrZWk+ItrDmQUOrcE6GSvIqx6TGpBWU=
In-Reply-To: <11b7sqn$32pr5$1@dont-email.me>
Content-Language: en-GB, nb-NO