svennykro
asked on
how to compare argv[] with a char
int main(int argc, char *argv[]) {
if (argv[1] == "--help") {
cout << "testing...";
}
}
if (argv[1] == "--help") {
cout << "testing...";
}
}
try:
int main(int argc, char *argv[]) {
if( strcmp(argv[1] ,"--help") == 0 ) {
cout << "testing...";
}
}
int main(int argc, char *argv[]) {
if( strcmp(argv[1] ,"--help") == 0 ) {
cout << "testing...";
}
}
ASKER
I get an error:
Thread stopped
Fault: access violation at 0x40118d: read of address 0x0
I use win2k and borland 5.02
Thread stopped
Fault: access violation at 0x40118d: read of address 0x0
I use win2k and borland 5.02
ASKER
But when I write test.exe --help it works..
ASKER
But when I write test.exe --help it works..
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";
}
}
You can use strcpy:
int main(argc, char *argv[])
{
if( strcmp(argv[0] ,"--help") == 0 )
{
cout << "help";
}
}
ASKER
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
Thread stopped
Fault: access violation at 0x40118d: read of address 0x0
ASKER
romanm allready proposed that solution..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
romanm was the first man to solution!
He gets the points!
He gets the points!
ASKER