OK, what's wrong with this?

int main(int argc, char* argv[1]) {  //declare argc and argv to check command-line arguments
  string arg = argv[1];              //convert char to string
  if (argc == 2) {                   //test if arg was entered
    if (arg == "test") {             //test if arg is 'test'
      printf("you entered the arg 'test'. \n");
      else
        printf("you didn't enter the arg 'test'. \n");
    }
    else
      printf("you entered no args. \n");
  }
  return 0;
}

g++ compiler complains a lot about my placement of the else's and }'s... I can't figure it out, besides putting an else before the nested if, but I want the nested if to be executed if the first one returns TRUE, not false...

Thank you very much in advance for any help!

if else statements do not get warped in brackets like you have here. the way they should look is like this

if (something)
{
    // code here
}
else
{
    // more code here
}

// and for nested statements
if (something)
{
    if (somethingelse)
    {
        // some code here
    }
    else
    {
        // else for somethingelse
    }
}
else
{
    // else for something
}

Also subtle but still there, this part :

char* argv[1]

creates a array of char pointer, with size 1, thus only index 0 is valid. That means
that this code :

string arg = argv[1];

is a index out of bounds.

Also subtle but still there, this part :

char* argv[1]

creates a array of char pointer, with size 1, thus only index 0 is valid. That means
that this code :

string arg = argv[1];

is a index out of bounds.

The array size in a function parameter is ignored because array notation in the parameter list is a syntactic convenience. The array is really just a pointer.

int main(int argc, char *argv[1])

// is the same as

int main(int argc, char **argv)

Misleading, maybe, but accessing argv[1] is not an out of bounds access provided argc is at least 2. The real problem here is accessing argv[1] *before* testing argc. ;)

And the other problem is that argv is declared as char*[1] instead of char**.

The array size in a function parameter is ignored because array notation in the parameter list is a syntactic convenience. The array is really just a pointer.

int main(int argc, char *argv[1])

// is the same as

int main(int argc, char **argv)

Misleading, maybe, but accessing argv[1] is not an out of bounds access provided argc is at least 2. The real problem here is accessing argv[1] *before* testing argc. ;)

I don't see how, char*argv[1] is the same as char**argv. Arrays are not pointers. Either
he's wrong for putting that "char*argv[1]" there, or I'm not seeing something here.

> Arrays are not pointers.
Except when they are. ;)

After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively.

> Arrays are not pointers.
Except when they are. ;)

oh I see what you mean. Thanks for that clarification.

@OP, this code

if(arg == "test"){...}

is not comparing the content. Its comparing addresses. Use strcmp like so :

if( strcmp(arg,"test") == 0) { ... }

is not comparing the content

It is, since arg is a string in his code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.