Solved

how to compare argv[] with a char

Posted on 2001-09-05
11
515 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

20 Experts available now in Live!

Get 1:1 Help Now