Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #147273
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: How to use a struct with qsort()? |
| Date | 2019-10-27 01:48 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87wocrrp22.fsf@bsb.me.uk> (permalink) |
| References | (4 earlier) <BkKqF.69070$1A2.65981@fx07.iad> <qovf0n$1lfh$1@gioia.aioe.org> <kUUsF.1229971$WI2.370840@fx34.am4> <qp1mtt$1f9h$1@gioia.aioe.org> <vC3tF.55417$SW2.31092@fx24.iad> |
DFS <nospam@dfs.com> writes:
> On 10/26/2019 10:58 AM, Manfred wrote:
<cut>
>> and his/her remark about books being unhelpful suggest limited knowledge of C)
>
>
> Let me show you an example:
>
> Bart posted a modified example of my code. In it he used memset this way:
>
> #define N_LETTERS 26
> unsigned char sc[N_LETTERS];
> memset(sc, 0, N_LETTERS);
>
>
> A tutorialspoint.com webpage does it similarly:
That's not a book, of course, but I think tutorials were also
mentioned. I agree with you here. There are so many awful online
tutorials that it is rarely a good idea to rely on them.
> char str[50];
> memset(str,'$',7);
>
>
> Should be simple, right?
Yes, it should be. The tutorial is, at best, very misleading.
> 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);
I defence of the (pretty bad) tutorialspoint.com page it describes
memset as a string function (so it's not encouraging its use for int
arrays) and it does give some hints about what memset really does in the
more general case. It says, for example, that the third parameter is
the number of bytes to be set.
> Doesn't work. The code compiles (tcc Windows) with no warnings, but
> it doesn't execute.
You are not showing us the real code. The memset you show here is
harmless as the array is zeroed by the initialisation. I think the
memset is used elsewhere in the code to get back to the initial state of
wsvals.
Every tutorial about memset should show something like
int wsvals[26];
...
memset(wsvals, 0, sizeof wsvals);
There is often no need to repeat either the length or the type of the
array.
> Finally on stackoverflow I find this:
> "Note that for memset you have to pass the number of bytes, not the
> number of elements because this is an old C function:
>
> So I do: memset(wsvals,0,26 * sizeof(int));
but, here, memset(wsvals, 0, sizeof wsvals) works with no change, even
if you switch the type of the array elements to some other integer type.
With dynamically allocated storage, you usually have to do the
multiplication but you can still use a type-agnostic form like this:
ptr = malloc(n_elements * sizeof *ptr);
...
memset(ptr, 0, n_elements * sizeof *ptr);
There are lots of caveats here (C is not a uniform language), but I'll
leave them to others who might want to point then out -- unless you want
a fuller explanation.
> And it works.
>
> Most of the descriptions on the web say memset "is used to fill a
> block of memory with given/particular value." No mention of the
> datatype.
>
> Why do I need to also use sizeof() for an int but not a char?
Short answer: because sizeof(char) == 1. Longer answer: sizeof is
defined to gave the number of char objects that make up the
representation of objects of some type. C, quite reasonably, calls
these "bytes", but on some very odd machines that can be a bit
surprising. On the vast majority of systems, int objects are
represented in memory by 2 or 4 consecutive bytes of storage.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 13:35 -0400
Re: How to use a struct with qsort()? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-10-19 10:22 -0700
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 14:19 -0400
Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-19 19:36 +0100
Re: How to use a struct with qsort()? Melzzzzz <Melzzzzz@zzzzz.com> - 2019-10-19 18:44 +0000
Re: How to use a struct with qsort()? Barry Schwarz <schwarzb@delq.com> - 2019-10-19 10:23 -0700
Re: How to use a struct with qsort()? Ike Naar <ike@sdf.lonestar.org> - 2019-10-19 19:09 +0000
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 15:51 -0400
Re: How to use a struct with qsort()? already5chosen@yahoo.com - 2019-10-19 13:09 -0700
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 16:26 -0400
Re: How to use a struct with qsort()? already5chosen@yahoo.com - 2019-10-19 13:51 -0700
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 17:24 -0400
Re: How to use a struct with qsort()? already5chosen@yahoo.com - 2019-10-19 14:49 -0700
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 18:26 -0400
Re: How to use a struct with qsort()? already5chosen@yahoo.com - 2019-10-19 16:32 -0700
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-20 09:22 -0400
Re: How to use a struct with qsort()? Paul <nospam@needed.invalid> - 2019-10-26 04:15 -0400
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-26 09:37 -0400
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-26 15:40 +0100
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-28 19:49 -0400
Re: How to use a struct with qsort()? Paul <nospam@needed.invalid> - 2019-10-29 01:59 -0400
Re: How to use a struct with qsort()? Paul <nospam@needed.invalid> - 2019-10-30 04:54 -0400
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-30 11:54 +0000
Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-30 08:58 -0700
Re: How to use a struct with qsort()? Öö Tiib <ootiib@hot.ee> - 2019-11-07 01:10 -0800
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-11-07 10:51 +0000
Re: How to use a struct with qsort()? Paul <nospam@needed.invalid> - 2019-10-26 15:03 -0400
Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-26 21:50 +0100
Re: How to use a struct with qsort()? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-04 16:02 -0800
Re: How to use a struct with qsort()? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-10-21 11:58 +0000
Re: How to use a struct with qsort()? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-10-23 04:51 -0700
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-19 23:25 +0100
Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-19 15:38 -0700
Re: How to use a struct with qsort()? David Brown <david.brown@hesbynett.no> - 2019-10-20 14:44 +0200
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-22 15:42 -0400
Re: How to use a struct with qsort()? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-10-23 05:47 -0700
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-23 16:40 +0100
Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-23 13:51 -0700
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-23 23:07 +0100
Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-23 15:50 -0700
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-24 01:20 +0100
Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-23 18:20 -0700
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-24 10:40 +0100
Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-24 11:00 -0700
Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-20 00:30 +0100
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-20 00:44 +0100
Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-20 01:27 +0100
Re: How to use a struct with qsort()? Manfred <noname@add.invalid> - 2019-10-25 20:30 +0200
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-25 16:07 -0400
Re: How to use a struct with qsort()? Ike Naar <ike@sdf.lonestar.org> - 2019-10-26 07:27 +0000
Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-26 02:11 -0700
Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-26 11:06 +0100
Re: How to use a struct with qsort()? Manfred <noname@invalid.add> - 2019-10-26 16:58 +0200
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-26 18:17 -0400
Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-26 16:08 -0700
Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-26 21:33 -0400
Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-26 22:10 -0700
Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-27 01:48 +0100
Re: How to use a struct with qsort()? Manfred <noname@invalid.add> - 2019-10-27 19:29 +0100
Re: How to use a struct with qsort()? Barry Schwarz <schwarzb4aarp@gmail.com> - 2019-10-27 13:09 -0700
csiph-web