Link to home
Start Free TrialLog in
Avatar of svennykro
svennykro

asked on

how to compare argv[] with a char

int main(int argc, char *argv[]) {

if (argv[1] == "--help") {
   cout << "testing...";
}
}
Avatar of svennykro
svennykro

ASKER

How can I get this to work???
try:
int main(int argc, char *argv[]) {

  if( strcmp(argv[1] ,"--help") == 0 ) {
    cout << "testing...";
  }
}
I get an error:

Thread stopped

Fault: access violation at 0x40118d: read of address 0x0

I use win2k and borland 5.02
But when I write test.exe --help it works..
But when I write test.exe --help it works..
You are comparing the pointer value not the value refered by pointer

You can use strcpy:

int main(argc, char *argv[])
{
 if( strcmp(argv[0] ,"--help") == 0 )
 {
   cout << "help";
 }
}
I know that, but when I start the test program (test.exe) with test.exe --help it works, but my problem now is that when I just start it with test.exe there is an error:

Thread stopped

Fault: access violation at 0x40118d: read of address 0x0
romanm allready proposed that solution..
ASKER CERTIFIED SOLUTION
Avatar of romanm
romanm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you are only running test.exe the argument passed is NULL, strcmp fails and you get error specified below.

Thread stoped
Fault: access violation at 0x40118d: read of address 0x0

make a check on argv[1] for NULL,  if it is not null then only proceed.

romanm was the first man to solution!

He gets the points!