Results 1 to 4 of 4

Thread: Array of Strings in C++

  1. #1
    Join Date
    Oct 2008
    Posts
    73

    Array of Strings in C++

    I'm working on a program in c++, and I've come up against a problem that I can't figure out. I don't imagine that it's too difficult to solve, but it has been giving me trouble.

    What I would like to do is create an array of strings. More specifically, I would like to create an array of char* 's. And the only catch is that I will need to pass the array by reference to several methods. Although from my understanding of pointers, I imagine that this is how things are done inherently in c++.

    I would really appreciate if somebody could give me a snippet of code where an array of char* is declared and assignments are made. I've looked all over, but haven't had any success in finding anything.

  2. #2
    Join Date
    Oct 2008
    Posts
    24

    Re: Array of Strings in C++

    Are you really sure you want to do that? This would be a lot easier to do with a vector of real honest-to-gosh C++ style strings. Then you don't have to mess with pointers, which are easy to make mistakes with (as I'm sure you're aware )

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    // pass the vector of strings by reference (note the &)
    // so no copying is needed
    
    void Display (const vector<string>& v)
    {
    
    // Vectors "know" how big they are, so you don't have to
    // pass the size separately.
    
    for (int k = 0; k < v.size(); ++k)
    {
    cout << "String #" << k << ": " << v[k] << endl;
    }
    }
    
    int main ()
    {
    // Create this vector with a specific size
    
    vector<string> myStrings(7);
    
    // Each string automatically expands to accommodate its characters
    
    myStrings[0] = "Hi";
    myStrings[1] = "I";
    myStrings[2] = "am";
    myStrings[3] = "a";
    myStrings[4] = "vector";
    myStrings[5] = "of";
    myStrings[6] = "strings";
    
    Display (myStrings);
    
    // Create this vector with size zero, and let it grow to
    // accommodate the data
    
    vector<string> moreStrings;
    
    moreStrings.push_back("I");
    moreStrings.push_back("am");
    moreStrings.push_back("another");
    moreStrings.push_back("vector");
    
    Display (moreStrings);
    
    return 0;
    }
    You can use pointers to pass data to functions in C++, but I personally
    prefer to use references, as in the example above.

  3. #3
    Join Date
    Mar 2008
    Posts
    82

    Re: Array of Strings in C++

    You can do it in the most obvious way. Here is some code:

    Code:
    void print_strings(const char *arr[], const int arr_sz)
    {
    for (int i=0; i < arr_sz; i++)
    std::cout << arr[i] << std::endl;
    }
    
    const int arr_sz = 3;
    const char *arr[sz] = { "first", "second", "third" };
    print_strings(arr);
    arr[0] = "first modified";
    print_strings(arr);
    Is this what you were looking for?

  4. #4
    Join Date
    Jun 2008
    Posts
    144

    Re: Array of Strings in C++

    No one has given you exactly what you have asked for so here it is, it might
    be what you want, but I doubt it.

    Code:
    void func(char* (&array)[3]);
    
    int main()
    {
    char* array[3] = { "apple", "pear", "orange" };
    func(array);
    }
    
    void func(char* (&array)[3])
    {
    for (int i = 0; i < 3; ++i)
    cout << array[i] << '\n';
    }
    The strange syntax for the parameter of func is how you pass an array by
    reference.

Similar Threads

  1. Files and strings in C
    By An1990 in forum Software Development
    Replies: 2
    Last Post: 10-05-2012, 10:11 PM
  2. How to use Strings in Python
    By Edmund-Windows in forum Software Development
    Replies: 5
    Last Post: 31-12-2010, 06:25 AM
  3. Strings in JavaFX
    By Messenger in forum Software Development
    Replies: 3
    Last Post: 16-07-2010, 04:57 PM
  4. Struct and Strings in C
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 05-04-2010, 12:31 PM
  5. C++ Array of Strings and Conversion !!
    By bihangteen in forum Software Development
    Replies: 6
    Last Post: 02-02-2009, 11:13 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,520,940.26468 seconds with 17 queries