how to compare argv[] with a char

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

if (argv[1] == "--help") {
   cout << "testing...";
}
}
svennykroAsked:
Who is Participating?
 
romanmConnect With a Mentor Commented:
ok.
before you do the strcmp( argv[1]...
check if argc > 1
if not the argv[1] is an invalid address and will cause a crash...
ex:

if( argc > 1 ){
  if ( strcmp(argv[1],"--help") == 0 ){
    cout << "...";
  }
}
0
 
svennykroAuthor Commented:
How can I get this to work???
0
 
romanmCommented:
try:
int main(int argc, char *argv[]) {

  if( strcmp(argv[1] ,"--help") == 0 ) {
    cout << "testing...";
  }
}
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
svennykroAuthor Commented:
I get an error:

Thread stopped

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

I use win2k and borland 5.02
0
 
svennykroAuthor Commented:
But when I write test.exe --help it works..
0
 
svennykroAuthor Commented:
But when I write test.exe --help it works..
0
 
sudhirsCommented:
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";
 }
}
0
 
svennykroAuthor Commented:
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
0
 
svennykroAuthor Commented:
romanm allready proposed that solution..
0
 
sudhirsCommented:
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.

0
 
svennykroAuthor Commented:
romanm was the first man to solution!

He gets the points!

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.