Solved

how to compare argv[] with a char

Posted on 2001-09-05
11
517 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

803 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