[Solved] Using int main(int argc, char **argv) in c++ [closed]

You haven’t actually provided code that exhibits your problem but, to answer your question, ways to pass argv[2] as a string to a function include #include <cstring> #include <iostream> void func(const char *s) { // treat s as a zero terminated string if (std::strcmp(s, “Hello”) == 0) { std::cout << “You said hello\n”; } } … Read more

[Solved] Read a file using argv

You can run the program as: IFS=$’\n’ ./sort.exe $(cat file.txt) Each line of the file will then be an argument to the program (setting IFS to just newline prevents spaces in the file from being argument delimiters). Then the program can loop over argv (except for argv[0], which contains the program name) to get all … Read more

[Solved] What is the output of this C code (run without any command line arguments)?

Quoting shamelessly from the C11 spec, chapter ยง5.1.2.2.1, (emphasis mine) โ€” The value of argc shall be non-negative. โ€” argv[argc] shall be a null pointer. โ€” If the value of argc is greater than zero, the array members argv[0]through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment … Read more