TIP: Click on subject to list as thread! ANSI
echo: locsysop
to: Paul Edwards
from: Bob Lawrence
date: 1996-11-24 08:38:24
subject: Bloody C

BL> I want to read a line out of FILES, put it in a pointer array,
 BL> sort the line using the first 4 characters, and then write the
 BL> sorted array to the new file.

 PE> Time for another use of char **. :-)

  Synchronicity is weird, isn't it? There was no way I could have
known I was going to find a use for that when I told you how useless
it was... and here it happens a day later! Jung was right: time is not
what we think it is. I reckon it runs a bit backwards as well.

 PE> There is an example program, xysort.c in OZPD, which does
 PE> exactly what you are doing. 

  I don't expect it will help though. You C programmers do it in Urdu
and I'm only fluent in Hindi.

... I thought I had a devious mind. Fiendishly clever, these Chinese. 

  Jeeze... I wish I'd seen that earleir.

 BL> return(strcmp((char *)a,(char *)b));

 PE> If you had a char list[500][50]; and were trying to sort it,
 PE> then the above method would work.

  Like a bought one...

 PE> You're not, you're trying to sort char *list[500]. To do that,
 PE> you need: 

 PE> return(strcmp(*(char **)a, *(char **)b));

  WHAAATT!!????? What the fuck does THAT mean?

  We're typcasting "a" as a pointer to a character pointer, and then
dereferencing it? JEESUZ!! I give up!! Are we passing the data behind
the pointer-pointer, or the pointer behind the data-pointer?

  I was already thinking along those lines, but I never would have 
guessed that in a hundred years. We have to typecast a pointer to the
malloc() pointer and then dereference it to get the pointer back? 
Jeesus!

 BL> How the bloody hell can I make it sort on just the first few
 BL> characters in each line?

 PE> I think you asked the wrong question. The answer to that
 PE> question is use strncmp() and specify 2, 3, 4 or whatever as
 PE> the length to look at. 

  It doesn't quite work like that. If each string being sorted is not
the same length, variable lengths get passed as a and b. I wrote my
own version of strncpy but I'm still as confused as buggery. It seems
that qcopy() needs constant-length strings. I can't even make it work
with a full array.

 BL> sort_function() it won't let me truncate the strings because
 BL> it's passing void pointers!

 PE> You can truncate strings simply by going *(p + 2) = '\0'. I
 PE> don't think you want to truncate strings though.

  By that stage I was desperate! 

Regards,
Bob
___ Blue Wave/QWK v2.12
@EOT:

---
* Origin: Precision Nonsense, Sydney (3:711/934.12)
SEEN-BY: 711/934 712/610
@PATH: 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.