🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #624

Re: Taming The Switch-Statement

#624
From: bart
Date: Wed, 21 Oct 2026 01:04
95 lines
5241 bytes
On 20/10/2026 13:58, David Brown wrote:
> On 20/10/2026 13:41, bart wrote:

> But let us take a concrete example.  These network functions are from 
> the LWIP network stack, which is designed to be used on small embedded 
> systems that may not have any kind of OS - so it uses call-backs for 
> asynchronous programming.
> 
> An example API function is this :
> 
> err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr,
>                          dns_found_callback found, void *callback_arg)
> 
> The returned "err_t" can be "ERR_OK" (indicating the "hostname" is in 
> the DNS cache and *addr is set to the IP), "ERR_ARG" for argument 
> errors, or "ERR_INPROGRESS" to indicate that the stack is looking up the 
> DNS name, and will call the callback when there is a final result or 
> timeout.
> 
> 
> Without typedef's, that function signature would be something like :
> 
> signed char dns_gethostbyname(const char *hostname, struct ip_addr
>      *addr, void (*found)(const char *name,    const struct ip_addr
>      *ipaddr, void *callback_arg), void *callback_arg);
> 
> 
> Do you expect anyone to believe you if you claim this is easier to read 
> than the real declaration using typedefs?  I'll believe you if you say 
> you find the original hard to read - but not if you claim the 
> "untypedef'ed" version is clearer.

Neither are great. But let's first lay them out better:

  err_t
     dns_gethostbyname(
       const char *hostname,
       ip_addr_t  *addr,
       dns_found_callback found,
       void       *callback_arg
     );

Here, one problem is that there are 3 types that I don't know and would 
have to look up. The long version at least has all the info in one place:

  signed char
     dns_gethostbyname(
       const char     *hostname,
       struct ip_addr *addr,
       void (*found)(const char *name, const struct ip_addr *ipaddr, 
void *callback_arg),
       void           *callback_arg
     );

That signed char return is unusual; why not 'int'? (How would you even 
print an 'err_t' value anyway)?

That 'ip_addr_t' turns out to be a struct. For those I would use 
typedefs anyway; I rarely use struct tags and you have to invent an 
identifier in either case.

Then we come to the function pointer, which is a quite a chunky one, but 
let's work with it. Here, I would question whether you really need those 
types in there, and especially those names.

Presumably you have a regular function of your own with the same 
signature? You anyway never directly 'call' this parameter with 
arguments; you just pass it the address of your handler.

Or do you use that typedef as documentation?

Where does the info come from anyway; does the library export a suitable 
typedef? In that case you might as well use it. But if not, info must 
come from elsewhere.

Anyway with some tweaks in place, using the struct typedef and using an 
inline function pointer type, it would look like this:

  signed char
     dns_gethostbyname(
       const char *hostname,
       IP_addr    *addr,
       void      (*found)(char*, IP_addr*, void *),
       void       *arg
    );

In this form, it is not wildly different from your short version.

So I would look at other ways of tidying up before resorting to 
extraneous typedefs.

But here, there may well be an extra reason to use a callback typedef; I 
don't have the full details.

Technical Details
Message-ID: <11b933e$3h3d0$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> <11arjmc$2qid7$1@dont-email.me> <11at5vi$5vu$1@reader2.panix.com> <11auad0$3no1k$1@dont-email.me> <11aupfl$3ul$1@reader2.panix.com> <11avmfb$55rt$1@dont-email.me> <11b2a6d$131h6$1@dont-email.me> <11b2hr6$15thq$1@dont-email.me> <11b2l06$16mof$1@dont-email.me> <11b2qv0$19h68$1@dont-email.me> <11b35gt$1cn63$2@dont-email.me> <11b557o$1kr9u$1@dont-email.me> <11b56uo$1q18k$5@dont-email.me> <11b6224$2ejik$1@dont-email.me> <11b774o$2m8c9$3@dont-email.me> <11b7k22$2v8e5$1@dont-email.me> <11b7ogt$2m8c9$5@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 02:04:47 +0100
Organization: A noiseless patient Spider
Lines: 95
Message-ID: <11b933e$3h3d0$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11arjmc$2qid7$1@dont-email.me>
 <11at5vi$5vu$1@reader2.panix.com> <11auad0$3no1k$1@dont-email.me>
 <11aupfl$3ul$1@reader2.panix.com> <11avmfb$55rt$1@dont-email.me>
 <11b2a6d$131h6$1@dont-email.me> <11b2hr6$15thq$1@dont-email.me>
 <11b2l06$16mof$1@dont-email.me> <11b2qv0$19h68$1@dont-email.me>
 <11b35gt$1cn63$2@dont-email.me> <11b557o$1kr9u$1@dont-email.me>
 <11b56uo$1q18k$5@dont-email.me> <11b6224$2ejik$1@dont-email.me>
 <11b774o$2m8c9$3@dont-email.me> <11b7k22$2v8e5$1@dont-email.me>
 <11b7ogt$2m8c9$5@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 01:04:47 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3706272"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19dB46cPCM259HNGvr7IQBq"; posting-host="c65e78a22dc81d6f5c20317bcf66a407"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:dBsYms2AmcBTI6atQ+BFJcHmNp0= sha256:mb0rQsOtH64XXJ7aixlbaMH4zN9RxddTq7OUl4HSMHA=
	sha1:VlyhB0iDFb+1MyoBoAAf+0JdOr0= sha256:tf+pFuKOtV9BrzGjwnlxF8v98IBUCZQi3/ItON9TNR4=
In-Reply-To: <11b7ogt$2m8c9$5@dont-email.me>
Content-Language: en-GB