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[]?
2 Answers
Gilles's answer gives you 90% of it, but the rest is on bash to do right.
$ arr=(foo bar 'Hello World!')
$ ./foo "${arr[@]}"
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.– N. F.Sep 24, 2012 at 0:57