Thread: How to pass an array of strings by reference?`

  1. #16
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    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.

  2. #17
    Registered User
    Join Date
    Jan 2003
    Posts
    16
    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...
    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] = "";
    }
    Second Program...

    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.

  3. #18
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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.
    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;
    }
    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:
    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;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  3. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM