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 22:10:12 -0700 Organization: None to speak of Lines: 65 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="a81003712e56c2310798229a9d42ce2b"; logging-data="31307"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+s/i1COvxx7i+ZTIfhHapO" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:BxrDZBxv25MFE9os3BUPEHuJ0nY= sha1:BFHfEPd9KIs+4RphuAgeZ1AV1Nc= Xref: csiph.com comp.lang.c:147276 DFS writes: > On 10/26/2019 7:08 PM, Keith Thompson wrote: >> DFS writes: >> [...] >>> So I try it in my code, to replace a small loop: >>> >>> int wsvals[26]={0}; (not a char as in previous examples) >>> memset(wsvals,0,26); >>> >>> Doesn't work. The code compiles (tcc Windows) with no warnings, but it >>> doesn't execute. >> >> I presume it does execute. It just doesn't do what you wanted it to do. > > Can't tell what it does. These are the letter counts /after/ > memset(vals,0,26), where vals is an int. The first one is correct > because it's the first count done after initialization. I can tell you exactly what it does. It zeros the first 26 *bytes* of wsvals. If sizeof (int) == 4 on your system, then that means it zeros the first 6 elements of the array, and the first half of the seventh (wsvals[6]), not touching the rest of the array. You didn't tell us that the declaration (with an initializer) and the memset call are separate, with memset possibly being called more than once. We can only diagnose the code you show us. If wsvals is initialized to all zeros at the point of definition (with the `= {0}` initializer), and then only the first quarter of the array is zeroed on subsequent iterations, then data will accumulate in the last 3/4 of the array. I presume the behavior you're seeing is consistent with that, but I'm not going to take the time to confirm it. Feel free to do so if you're curious. [...] > Why doesn't the compiler recognize the data type of the object being > passed to memset, and handle the sizing for you? > > So that memset(arr, v, N) would work for chars and ints. Because it can't. memset is a function, not a built-in language feature. Its first argument is of type void*, which carries no information about the type or size of the target object. That also means that you can use memset() to set every element of a char array to some non-zero value, but you can't do that with an int array, because it sets every *byte* to the specified value. For example: char char_array[N]; int int_array[N]; memset(char_array, 0x42, sizeof char_array); // sets each element to 0x42 memset(int_array, 0x42, sizeof int_array); // sets each byte to 0x42, which most likely sets // each element to 0x42424242 0 is the most commonly used value. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */