Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
Member
 
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.
Reply With Quote
  #2  
Old 25-10-2008
Member
 
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.
Reply With Quote
  #3  
Old 25-10-2008
Member
 
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?
Reply With Quote
  #4  
Old 25-10-2008
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 05:26 AM.