Reputation: 1150
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.
Users/venushka/Library/Developer/Xcode/DerivedData/testFunctions-blcisknaxkrqribioawblwrttsib/Build/Products/Debug
i executed myFunctions bash as
./testFunctions
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 below
Upvotes: 0
Views: 93
Reputation: 249153
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".
Upvotes: 5