0

I have C++ program. there i have used C++ . in order to run that program i'm passing values via unix terminal. But i couldn't run code snippet by passing args values.

  1. obtain bash build path as

Users/venushka/Library/Developer/Xcode/DerivedData/testFunctions-blcisknaxkrqribioawblwrttsib/Build/Products/Debug

  1. i executed myFunctions bash as

    ./testFunctions

  2. i pass args as

    ./testFunctions 1

please find below the code snippet i used

int main(int argc, const char * argv[]) {
    // insert code here...
    //std::cout << "Hello, World!\n";
    if (*argv[0] == 1) {
        std::cout << "Hello, World!\n";
        //test();
    }
    return 0;
}

when i gives 1 as arg my programe line hello world hasn't executed. what am i do wrong please help me. thank you . please find the image belowMy terminal image

1 Answer 1

5

This is wrong:

if (*argv[0] == 1)

argv[0] is a string which is the name of your program. Dereferencing it (with *) gives you the first character. It will never match 1 which is a non-printing ASCII code. Instead:

if (strcmp(argv[1], "1") == 0)

That will check if the first argument to the program (not its name) is the string "1".

Sign up to request clarification or add additional context in comments.

1 Comment

Can also use strtol(argv[1]) to compare ints.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.