TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: David Nugent
from: Frank Adam
date: 1996-06-13 11:12:00
subject: Find a word

G'Day David,
 
-=> Quoting David Nugent to Frank Adam <=-

 DN> Just out of interest, why cast the return from malloc()? If 
 DN> it is because it has no prototype, then you should do the 
 DN> /correct/ thing and include stdlib.h.

I have to cast it in Borland. In both TC2 and BC4 i'll get a 
"cannot assign void* to char* error" otherwise, it'll happen with both
ANSI, or Borland extensions used C or C++. 
I've given in now, and just use the casts .
Wrt stdlib, it is one of the libs i'd include as a reflex action,
if it or alloc.h is not there malloc() won't compile at all. 

 DN> Excuse my bent towards better readability :-)
And what's wrong with my codes' readability ? 
Dammit, i've "accidently" cut the evidence :)

 DN> char * In_Str(char const * buf, char const * wrd)
 DN> {
 DN> char *txt, *ptr =NULL;

 DN> if ((txt=strdup(buf)) != NULL)
 DN> {
 DN> char * pat;

 DN> if ((pat=strdup(wrd)) != NULL)
 DN> {
 DN> ptr = strstr(strlwr(txt),strlwr(pat));
 DN> free(pat);
 DN> }

 DN> free(txt);
 DN> }
 DN> return ptr;
 DN> }

I don't like strdup(), makes it a bit easy to forget to free the 
string,but i guess it's a matter of taste and habits.    
Have you seen the latest bug free version of strstr i've posted ? 
Paul was good enough to add the consts to it, but i don't understand
why would the local pointers have be constant.


 DN> Note the 'const' in the parameters. It is a good idea to 
 DN> define the interface to a function as clearly as possible, 
 DN> which is why const exists. It also allows you to use the 
I always thought the main functionality of a const was to keep the 
variables constant. 

                                                
 DN> If the compiler implements it and stack space isn't going 
 DN> to be a problem, you could use alloca() for the temporary 
 DN> strings. alloca() is usually significantly faster than the 
 DN> standard heap allocation functions, and freeing is 
 DN> automatic.
I haven't used it yet, mainly because TC2 does not implement it, and 
i'd spend 90% of programming time there(being a DOS IDE). 
BC4 does have it, so i'll have to keep it in mind, i normally do my 
final compile with it.It tends to do a better optimize.Not on size. :)

 DN> And yes, there are some non-ANSI functions above, but the 
 DN> non-ANSI functions used are both commonly available or 
 DN> trivial to implement.
Some ? Can i watch when you post this in c.l.c ? :-) 
Doesn't mean i don't like it:-)
       
  L8r Frank (fadam{at}ozemail.com.au).
  
___ Blue Wave/DOS v2.21
                         
            

--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/401 409 410 413
SEEN-BY: 711/430 808 809 932 934 712/515 713/888 714/906 800/1
@PATH: 635/544 50/99 711/808 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™.