Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: How to use a struct with qsort()? Date: Sat, 26 Oct 2019 02:11:35 -0700 Organization: None to speak of Lines: 29 Message-ID: References: <06add10e-1e30-4741-a20c-ff56363733fe@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="7704f66ce45f0c9bf0335096efaa4bdc"; logging-data="16254"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nR1wIzlXSJBwiH1jpu2oU" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:2YtTf39qbBb7enEXSyjgLa4uTeE= sha1:Z92Y7FJ7KXSw7NAB/zbkwobrxYY= Xref: csiph.com comp.lang.c:147239 Ike Naar writes: > On 2019-10-25, Manfred wrote: >> On 10/19/2019 10:26 PM, DFS wrote: >>> int primes[26] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, >>> 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101}; >>> char *charset = "abcdefghijklmnopqrstuvwxyz"; >> Since primes and charset are strictly coupled, it is a good idea to >> enforce consistency of their sizes as in: >> #include >> static const unsigned int primes[] = >> { >> 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, >> 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 >> }; >> static const char charset[] = "abcdefghijklmnopqrstuvwxyz"; >> static_assert(sizeof(primes)/sizeof(primes[0]) == >> (sizeof(charset)/sizeof(charset[0])-1), >> "charset and primes mismatch"); > > Why the "-1" ? > primes[] and charset[] have the same number of elements (26). No, charset[] has 27 elements. It's initialized from a string literal, so there's a trailing '\0'. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */