Article View: comp.lang.c
Article #755Re: variadic min and max, (any some and none, cat and more)
From: Richard Harnden
Date: Sun, 25 Oct 2026 10:55
Date: Sun, 25 Oct 2026 10:55
62 lines
2496 bytes
2496 bytes
On 25/10/2026 04:24, Richard Harnden wrote:
> On 23/10/2026 06:44, fir wrote:
>> when thinkong on variadic functions not much come to mind but
>> only recently i noticed min or max would be good for variadic
>>
>> the ai wrote me such variadic min
>
> I don't think that I'd bother with stdarg at all.
>
> Just use a string of ints ...
>
> int min(const int *n)
> {
> int m = INT_MAX;
>
> while ( *n )
> {
> if ( *n < m ) m = *n;
>
> n++;
> }
>
> return m;
> }
>
>> int a=Min(4,10,3,7,-2); // -2
>
> int a = min((int[]){10,3,7,-2}));
>
>
That has undefined behaviour - unfortunately, the kind that does what I
expected :(
You either need to pass the size ...
int min(const int *n, size_t z)
{
int m = INT_MAX;
for (size_t i=0; i<z; i++)
if ( n[i] < m ) m = n[i];
return m;
}
Or have a NULL terminated pointer to pointer to int ...
int min(int **n)
{
int m = INT_MAX;
while ( *n )
{
if ( **n < m ) m = **n;
n++;
}
return m;
}
Technical Details
Message-ID:
<11bkn79$3he92$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:
<11bes8b$1gncr$1@dont-email.me> <11bk09r$3acgl$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: Richard Harnden <richard.nospam@gmail.invalid> Newsgroups: comp.lang.c Subject: Re: variadic min and max, (any some and none, cat and more) Date: Sun, 25 Oct 2026 10:55:36 +0000 Organization: A noiseless patient Spider Lines: 62 Message-ID: <11bkn79$3he92$1@dont-email.me> References: <11bes8b$1gncr$1@dont-email.me> <11bk09r$3acgl$1@dont-email.me> Reply-To: nospam.harnden@invalid.com MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 25 Oct 2026 10:55:37 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3717410"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nNP40EUvc0cFAy42D2OOSMLYDl9pwY9s="; posting-host="87e71cb45a677844bec49ebc6519ca4d" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:FCFmpC+XyUNvIu/SVxFv09HEJ5Y= sha256:2t9xc/InIp043sn943jI1kEE2pgRfFnkNkBGBFMFRho= sha1:EZ1qV8CYTLdDXcjL/9+zAka3kXM= sha256:nFBzLM16N9/4CnSmqJ64qyd/J7BmjttCldzuk0tF6hU= Content-Language: en-GB In-Reply-To: <11bk09r$3acgl$1@dont-email.me>