Solved

how to compare argv[] with a char

Posted on 2001-09-05
11
516 Views
Last Modified: 2007-11-27
int main(int argc, char *argv[]) {

if (argv[1] == "--help") {
   cout << "testing...";
}
}
0
Comment
Question by:svennykro
  • 7
  • 2
  • 2
11 Comments
 

Author Comment

by:svennykro
ID: 6456573
How can I get this to work???
0
 
LVL 3

Expert Comment

by:romanm
ID: 6456576
try:
int main(int argc, char *argv[]) {

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

Author Comment

by:svennykro
ID: 6456613
I get an error:

Thread stopped

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

I use win2k and borland 5.02
0
 

Author Comment

by:svennykro
ID: 6456616
But when I write test.exe --help it works..
0
 

Author Comment

by:svennykro
ID: 6456618
But when I write test.exe --help it works..
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:sudhirs
ID: 6456621
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
 

Author Comment

by:svennykro
ID: 6456629
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
 

Author Comment

by:svennykro
ID: 6456636
romanm allready proposed that solution..
0
 
LVL 3

Accepted Solution

by:
romanm earned 50 total points
ID: 6456652
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
 

Expert Comment

by:sudhirs
ID: 6456657
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
 

Author Comment

by:svennykro
ID: 6456714
romanm was the first man to solution!

He gets the points!

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now