[comp.sys.ibm.pc] why does this compare fail in TC2.0?

cpcahil@virtech.UUCP (Conor P. Cahill) (08/29/89)

In article <127@cica.cica.indiana.edu>, burleigh@cica.cica.indiana.edu (Frank Burleigh) writes:
> 1. in a test file that has some missings, the compare fails every time,
>    even though i can see via tc's watch that fld actually holds ".," with
>    NULL in fld[2].  thus, execution always drops through to the else
>    clause.  i *have* a different version:
>  
>        if( fld[0] == '.' && fld[1] == ',' ) ...
>  
>    which works correctly.    why does the compare fail?

Because C comparisons only work on scalar values.  the compare that 
you were using is actually comparing the pointer fld to the address
of the ".," constant (this should never be true).





-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

g-tookey@rocky.cs.wisc.edu. (Richard Schaut) (08/29/89)

In article <127@cica.cica.indiana.edu>, burleigh@cica.cica.indiana.edu (Frank Burleigh) writes:
> 1. in a test file that has some missings, the compare fails every time,
>    even though i can see via tc's watch that fld actually holds ".," with
>    NULL in fld[2].  thus, execution always drops through to the else
>    clause.  i *have* a different version:
>  
>        if( fld[0] == '.' && fld[1] == ',' ) ...
>  
>    which works correctly.    why does the compare fail?

If you want to compare strings, use strcmp or strncmp.  Also, for
what you are doing, look up strtol and sscanf in the TC manuals.

Finally, a bit of advice I see quite often on the Borland forum on
compuserve: turn on ALL warnings, prototype EVERYTHING, and get
your code to compile without any warnings and without using any casts
(i.e. by including the appropriate header files).  By doing that, you 
let the compiler catch most of your errors.  No sense in trying to do 
that which the compiler can do better :-).


Rick
Please send e-mail to: schaut@madnix.UUCP
ArpaNet: madnix!schaut@cs.wisc.edu
UseNet: ...uwvax!astroatc!nicmad!madnix!schaut
             {decvax!att}!

I am posting this through a friend's account.  His consent to my use of his
account in no way implies his consent to responsibility for the opinions
expressed herein.

coy@ssc-vax.UUCP (Stephen B Coy) (08/30/89)

In article <127@cica.cica.indiana.edu>, burleigh@cica.cica.indiana.edu (Frank Burleigh) writes:
> this is driving me fairly crazy.  i'm using turbo c 2.0.
>  
>         if( fld == ".," )
>             ptr_tmp[i] = strtod( "+NAN", NULL );    /*missing value*/

Remember, fld is a pointer.  ".," is also a pointer.  What you are
comparing here are two pointer values.  Since fld points to part
of your input string and ".," points to the constant string ., in
memory they will never be equal.  What you want in this case is to
compare the first two characters pointed to by the pointers.

	if(strncmp(fld, ".,", 2) == 0)		/* if equal */
		...

>        if( fld[0] == '.' && fld[1] == ',' ) ...
>    which works correctly.

Yes, it should.  In this case you are comparing chars which is what
you want.

> Frank Burleigh  burleigh@cica.cica.indiana.edu
> USENET: ...rutgers!iuvax!cica!burleigh BITNET: BURLEIGH@IUBACS.BITNET
> Department of Sociology, Indiana University, Bloomington, Indiana 47405

Stephen Coy
uw-beaver!ssc-vax!coy