Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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[]?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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

$ arr=(foo bar 'Hello World!')
$ ./foo "${arr[@]}"
share|improve this answer

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
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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