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

higgin@cbmvax.commodore.com (Paul Higginbottom - CATS) (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 +");
$if (a = '0') printf("a equals '0'");
$if (a = 0) printf("a equals 0");
$}

Nice to catch an easier one to answer once in a while...

The problem is that argv[1] is in fact a POINTER to the argument string
of characters, and not a character itself.

char a = argv[1][0];	/* might work */

Otherwise:

char *cp = argv[1];
char a = *cp;

	Good luck.
	Paul.

phil@adam.adelaide.edu.au (Phil Kernick) (02/08/91)

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 +");
>if (a = '0') printf("a equals '0'");
>if (a = 0) printf("a equals 0");
>}

Lots of things wrong here.

        1.  This program will not even compile because
                char a = argv[1]; is not a valid declaritor.

        2.  You should check that argv[1] is valid by making sure
            that (argc > 1).  Otherwise (argv[1] == 0) and you do
            *not* want to dereference that.

        3.  argv[1] is a pointer to a pointer to a char, what you
            want is:
                char a;
                a = *argv[1];

        4.  The if (a = ...) statement is an assignment, not a
            comparison.  You need statements like:
                if (a == '+')  printf("a equals +");

        5.  All of the above asside, you *cannot* have a lone '+'
            as a parameter to a program, because AmigaDos eats it.
            See the documentation for the RUN command.


Phil.
-- 
o|      ///   Phil Kernick              EMail:  phil@adam.adelaide.edu.au    |o
 |     ///    Departmental Engineer     Phone:  +618 228 5914                |
o| \\\///     Dept. of Psychology       Fax:    +618 224 0464                |o
 |  \///      University of Adelaide    Mail:   GPO Box 498 Adelaide SA 5001 |