Mail Archives: djgpp/1998/05/27/12:45:36
Clint Allen wrote in message ...
:Anyone who can help on this?
:I am trying to use command line options in my program. Here's what I've got
:so far:
: if (argv[1] == "/nts")
=============
This is a question about C, not about djgpp.
Get the FAQ for comp.lang.c from ftp://rtfm.mit.edu/pub/usenet-by-group.
It has answers to this and many other questions, e.g.
8.2: I'm checking a string to see if it matches a particular value.
Why isn't this code working?
char *string;
...
if(string == "value") {
/* string matches "value" */
...
}
A: Strings in C are represented as arrays of characters, and C
never manipulates (assigns, compares, etc.) arrays as a whole.
The == operator in the code fragment above compares two pointers
-- the value of the pointer variable string and a pointer to the
string literal "value" -- to see if they are equal, that is, if
they point to the same place. They probably don't, so the
comparison never succeeds.
To compare two strings, you generally use the library function
strcmp():
if(strcmp(string, "value") == 0) {
/* string matches "value" */
...
}
==============
- Raw text -