4

I wanted to know, if its possible to create an array_of_strings and send it to a C++ program, which has an argument string array_of_strings[]?

3

Gilles's answer gives you 90% of it, but the rest is on bash to do right.

$ arr=(foo bar 'Hello World!')
$ ./foo "${arr[@]}"
2

All programs receive an array of strings as its arguments. In C++, the arguments are the argv parameter of the main function. The first element of that array is the name of the program, the others are the arguments that you pass.

$ cat foo.cpp
#include <iostream>
int main (int argc, char *argv[]) {
    for (int i = 1; i < argc; i++)
        std::cout << argv[i] << std::endl;
    return 0;
}
$ g++ -o foo foo.cpp
$ ./foo hello world
hello
world
  • I wanted to mean, not writing it explicitly, rather save strings in a bash array, and send it to a C++ function. – MiNdFrEaK Sep 24 '12 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.