Why not use a vector<string> and pass it by reference instead?
Code:void funct( vector<string>& v ) { // you have a vector of strings by reference } int main() { vector<string> str_vec; funct(str_vec); return 0; }
Why not use a vector<string> and pass it by reference instead?
Code:void funct( vector<string>& v ) { // you have a vector of strings by reference } int main() { vector<string> str_vec; funct(str_vec); return 0; }
The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.
WOW, Thanks for all the replies.
I tried the suggestion by shiv_tech_quest, and it worked just fine.
I tried the following two programs and they both work with the desired result just as the one suggested by shiv_tech_quest.
All three programs work as desired, but are there any differences or advantages between the three, or is it just three ways to do the exact same thing. Is the code created by the three the same? I am pretty new to c++ and am trying to learn as much as possible.
Thanks again for all the help, the problem was for a larger program I am trying to write and will save me much typing of repetative routines.
James
First Program...
Second Program...Code:#include <string> #include <iostream> using namespace std; void goodbye(string ra[]); int main() { string a[3]; a[0] = "Hello"; a[1] = "There"; cout << a[0] << endl; cout << a[1] << endl; goodbye(a); cout << a[0] << endl; cout << a[1] << endl; return 0; } void goodbye(string ra[]) { ra[0] = "Goodbye"; ra[1] = ""; }
Code:#include <string> #include <iostream> using namespace std; void goodbye(string *pa); int main() { string a[3]; a[0] = "Hello"; a[1] = "There"; cout << a[0] << endl; cout << a[1] << endl; goodbye(a); cout << a[0] << endl; cout << a[1] << endl; return 0; } void goodbye(string *pa) { pa[0] = "Goodbye"; pa[1] = ""; }
Last edited by JamesMI; 01-25-2003 at 12:22 AM.
your first and second programs will be almost identical. The only time when you would notice a difference is that in foo(int arr[]) arr acts like a const pointer in that you cannot change the address it points to. Many people say const pointer when the really mean pointer to const.
Shiv's reference syntax is a new one on me, but I like it. It seems to invoke stronger typechecking, though this was all found by simple experimenting, I don't know how much of this is standard.Code:int * const const_ptr; const int *ptr_to_const; const_ptr++; // fails *const_ptr = 7; // ok ptr_to_const++; // ok *ptr_to_const = 7; // fails void foo(string *pa) { pa++; // works; } void bar(string ra[]) { ra++; // fails; }
Code:void byref(char (&arr)[10]) { cout << sizeof(arr) << endl; // outputs 10 } void normal(char arr[10]) { cout << sizeof(arr) << endl; // outputs 4== (sizeof(void *)) } ... char ten[10]; char nine[9]; char *p = new char[10]; byref(ten); // works byref(nine); // fails byref(p); // fails normal(ten); // works normal(nine); // "works" normal(p); // works;