Code: What am I doing wrong?

Code: What am I doing wrong?

Author

Message

Canada Ba#1 / 5

Code: What am I doing wrong?

I am trying to test a command line argument, unfortunately I always seem to get the false leg. I've tried numerous sytanx', but can't seem to come up with anything. Could someone please help me out on this?

If the user has a parameter of -al then it should execute the true leg.

Thanks for the help.

Canada Bass

"Remove the no_spam to reply."

Wed, 22 Dec 1999 03:00:00 GMT

Canada Ba#2 / 5

Code: What am I doing wrong?

Sorry for the add on, but I forgot to ask. How do you pass a command line parameter to a system call? In that I am unsucessful also.

Thanks again. Canada Bass

Quote:

>I am trying to test a command line argument, unfortunately I always >seem to get the false leg. I've tried numerous sytanx', but can't seem >to come up with anything. Could someone please help me out on this?

>If the user has a parameter of -al then it should execute the true >leg.

>Thanks for the help.

>Canada Bass

>"Remove the no_spam to reply."

Wed, 22 Dec 1999 03:00:00 GMT

Erik Jonsso#3 / 5

Code: What am I doing wrong?

Quote:

> I am trying to test a command line argument, unfortunately I always > seem to get the false leg. I've tried numerous sytanx', but can't seem > to come up with anything. Could someone please help me out on this? > if ("argv[1]" == "-al")

At first, you can't use "" around a symbol name. Then you can't compare strings like you do. In c a string is implemented as an array of chars. If you use the == operator like this you do in fact try compare the memory location of argv[1] and "-al".

The correct thing to do would be like this:

if(!strcmp(argv[1],"-al"))

where strcmp returns 0 if the strings are equal.

Wed, 22 Dec 1999 03:00:00 GMT

Cliff Rhode#4 / 5

Code: What am I doing wrong?

Quote:

> I am trying to test a command line argument, unfortunately I always > seem to get the false leg. I've tried numerous sytanx', but can't seem > to come up with anything. > main (int agrc, char *argv[]) > { > if ("argv[1]" == "-al")

You cannot compare strings directly in C. You must use a function:

if(!strcmp(argv[1], "-al"))

also, you don't need to put argv[1] inside quotation marks, it is already a pointer to a string (or NULL).