?
Solved

how to compare argv[] with a char

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

if (argv[1] == "--help") {
   cout << "testing...";
}
}
0
Comment
Question by:svennykro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 150 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

777 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