www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/30/02:01:09

Message-ID: <35C0098B.6835A285@mailexcite.com>
Date: Thu, 30 Jul 1998 01:50:04 -0400
From: Doug Gale <dgale AT mailexcite DOT com>
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: A DOUBT LOOK THIS
References: <35BFFED0 DOT 459E AT hotmail DOT com>
NNTP-Posting-Host: oshawappp84.idirect.com
Organization: "Usenet User"
Lines: 54
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp


A.S.A. Inc. wrote:

> GIVE ME YOUR HELP!!!!
>
> What's Wrong????
>
> main(int argc,char *argv[])
> {
> ScreenClear();
> puts(argv[1]);
>
> if (argv[1]=='the') printf("OK");  ----> Problens with IF
> else printf("Wrong");
>
> getchar();
> exit(0);
> }


Looks like you are mixed up on pointers. When you say "argv[1]" you are
getting the address of the first letter of the second parameter. Then
you are comparing the address of the parameter to 'the'. This, of
course, won't work. Also, if you don't use double quotes on the value
you are comparing to, the compiler converts 'the' into an integer, with
't' in the low byte, 'h' in the next most significant byte, and 'e' int
the low byte of the high word. Here is the corrected code:

main(int argc, char *argv[])
{
ScreenClear();
puts(argv[1]);

if  (strcmp(argv[1], "the") == 0)
    printf("OK");
else
    printf("Wrong");

fflush(stdout);        /* Do this because printf output is buffered! */

getch();
return 0;
}

You see? strcmp takes two pointers. A string with a double-quote
evaluates to the address of the first character of the string. The
address of the parameter and the address of "the" are passed to strcmp.
If the strings _pointed_to_ by the two pointers match, strcmp returns 0.

This is a little off-topic, but is more helpful than to post back
complaining "WRONG NEWSGROUP" :)


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019