[comp.sys.amiga.programmer] Compairing C values

manes@vger.nsu.edu ((Mark D. Manes), Norfolk State University) (02/06/91)

In article <rat.3380@hotcity.UUCP>, rat@hotcity.UUCP (P W) writes:
>         I have + as an input arguement via *argv[] and whenever I try to
> compare its value it comes out as a 0.  For instance...  If I compile a
> program like this:
> 
> #include <stdio.h>
> #include <math.h>
> 
> void main(int argc, char *argv[])
> 
> {
> 
>         And I type "test +" (I would name the file test) it would just print a
> equals 0.  Any help would be appereciated...
> char a = argv[1];
> if (a = '+') printf("a equals +");

Change = which means 'assign' to == which means 'equal to?'

> if (a = '0') printf("a equals '0'");
> if (a = 0) printf("a equals 0");
> }

 -mark=
     
 +--------+   ==================================================          
 | \/     |   Mark D. Manes                    "Mr. AmigaVision" 
 | /\  \/ |   manes@vger.nsu.edu                                        
 |     /  |   (804) 683-2532    "Make up your own mind! - AMIGA"
 +--------+   ==================================================
                     

ewing@tortuga.SanDiego.NCR.COM (David Ewing) (02/07/91)

Newsgroups: comp.sys.amiga.programmer
Subject: Re: Compairing C values
Summary: also assigning a char * to a char
References: <rat.3380@hotcity.UUCP> <606.27aede55@vger.nsu.edu>
Reply-To: ewing@tortuga.SanDiego.NCR.COM (David Ewing)
Distribution: na
Organization: NCR Corporation, Rancho Bernardo

>> void main(int argc, char *argv[])
>> {
>> char a = argv[1];
>> if (a = '+') printf("a equals +");
...

In addition to the use of the wrong operator as already mentioned
(ie, you used = instead of ==), you are assigning a char pointer to
a char :
  char a = argv[1];
argv is an array of char pointers, therefore argv[1] is a char
pointer, not a char.

This should either be :
  char a = *argv[1]; /* note : dereferencing the pointer */
  if (a == '+') {
     /* do whatever */
  }
if you are only concerned with the first character in the second
string (ie, this will accept "test +anything" as well as just
"test +")  --

or if you only want to accept "+" and not "+anything" :
  char *a = argv[1]; /* note a is now a char * */
  if (a)  { /* be sure argv[1] was not NULL !!! */
     if (strcmp(a, "+") == 0) {
     /* do whatever */
     }
  }

David Ewing
ewing@se-sd.sandiego.ncr.com
Newsgroups: comp.sys.amiga.programmer
Subject: Re: Compairing C values
Summary: 
Expires: 
References: <rat.3380@hotcity.UUCP> <606.27aede55@vger.nsu.edu>
Sender: 
Reply-To: ewing@tortuga.SanDiego.NCR.COM (David Ewing)
Followup-To: 
Distribution: 
Organization: NCR Corporation, Rancho Bernardo
Keywords: 

*********************************************
David A. Ewing
ewing@tortuga.sandiego.ncr.com
*********************************************