Mail Archives: djgpp/1998/07/30/02:01:09
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 -