Re: brain teasers
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Fri, 06 Apr 2007 07:37:14 +0000
Daniel Rudy said:
<snip>
Man, you guys are flat ruthless on my coding algorithms and style. My
original one was shot down, then my malloc one caused an argument...
Don't you think that's a good thing?
All of us have been shot down in flames a few times here in c.l.c, and
it has generally been a useful experience.
In
taking everyone's suggestions into account. I have devised the
following routine:
void swap(void *a, void *b, size_t size)
You'll need a header for that size_t.
{
size_t i; /* generic counter */
unsigned char tmp; /* temp */
unsigned char *d1, *d2; /* data pointers */
d1 = a;
d2 = b;
for (i = 0; i < size; i++)
{
tmp = *d1;
Your indenting is broken. <g,d&rvf>
That one doesn't use malloc, swaps 1 byte at a time, and can be used
with any datatype. It will not increment d1 and d2 beyond the size
either.
I think you could profitably swap more bytes at a time than that, using
memcpy. Who told you swapping many bytes at once was a bad idea?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.
- Follow-Ups:
- Re: brain teasers
- From: Daniel Rudy
- Re: brain teasers
- References:
- brain teasers
- From: viv342
- Re: brain teasers
- From: Martin Ambuhl
- Re: brain teasers
- From: rohitjogya@xxxxxxxxx
- Re: brain teasers
- From: Martin Ambuhl
- Re: brain teasers
- From: Daniel Rudy
- Re: brain teasers
- From: Ian Collins
- Re: brain teasers
- From: user923005
- Re: brain teasers
- From: Keith Thompson
- Re: brain teasers
- From: user923005
- Re: brain teasers
- From: Keith Thompson
- Re: brain teasers
- From: user923005
- Re: brain teasers
- From: user923005
- Re: brain teasers
- From: Ian Collins
- Re: brain teasers
- From: Daniel Rudy
- brain teasers
- Prev by Date: Re: c doubt
- Next by Date: Re: c doubt
- Previous by thread: Re: brain teasers
- Next by thread: Re: brain teasers
- Index(es):
Relevant Pages
|