argv[] comparism - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

argv[] comparism

tito2502tito2502 Posts: 2Member
Hi, I'm trying to make a program which accepts command line parameters.

I know argv[0] is the executable name so

if (argv[2] == "-c")
{Do this code}

but my compiler says comparing a pointer to a char is forbidden,

So, can someone turn my statement above into code that works?

Comments

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : Hi, I'm trying to make a program which accepts command line parameters.
    :
    : I know argv[0] is the executable name so
    :
    : if (argv[2] == "-c")
    : {Do this code}
    :
    : but my compiler says comparing a pointer to a char is forbidden,
    :
    : So, can someone turn my statement above into code that works?
    :

    [blue]You want to use [italic]strcmp[/italic] to compare the argument. As you have it above, the value "-c" is a [italic]string literal[/italic] and in the context of the [italic]if[/italic] statement where you are comparing it with argv[2], it will resolve to the address of that [italic]string literal[/italic] stored in memory. As stated, the end result is that your [italic]if[/italic] statement is comparing the addresses of two different memory segments which will [b]NEVER[/b] be equal... hence you need to use [italic]strcmp[/italic].[/blue]
  • tito2502tito2502 Posts: 2Member
    I tried

    while (strcmp(argv[2],"c") == 0)
    {code}

    {code2}


    So code2 should execute when the while statement is false, this all works fine except it won't work without any arguments.

    So

    program c
    Works fine

    but
    program
    gives "segmentation error"
  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : I tried
    :
    : while (strcmp(argv[2],"c") == 0)
    : {code}
    :
    : {code2}
    :
    :
    : So code2 should execute when the while statement is false, this all works fine except it won't work without any arguments.
    :
    : So
    :
    : program c
    : Works fine
    :
    : but
    : program
    : gives "segmentation error"
    :

    [blue]Post a small sample program that demonstrates your problem. Remember to use code tags when posting code samples.[/blue]
  • stoberstober Posts: 9,765Member ✭✭✭
    :it won't work without any arguments.
    :

    you need to check the value of argc to make sure there were sufficient arguments. argv == 1 when no arguments specified on the command line.
    [code]
    if(argc > 1)
    {
    // blabla
    }
    [/code]
  • Gregry2Gregry2 Posts: 607Member
    [b][red]This message was edited by Gregry2 at 2006-7-25 17:32:35[/red][/b][hr]
    : :it won't work without any arguments.
    : :
    :
    : you need to check the value of argc to make sure there were sufficient arguments. argv == 1 when no arguments specified on the command line.
    : [code]
    : if(argc > 1)
    : {
    : // blabla
    : }
    : [/code]
    :

    Also, if argc>2 (therefore, argv[2] is set), the condition

    [blue]while[/blue](strcmp(argv[2],"-c")==0){
    ...
    }

    , if [red]true[/red], will be an infinite loop(unless you've got some break or something), for neither the arguments can change and "-c", well its a literal, so it won't either. Use a simple [blue]if[/blue] instead, unless you need a loop...but then, you might want to structure that part differently...
    {2}rIng


Sign In or Register to comment.