how to send an array to another funtion

I have a string array in my main function, and i want to be able to use that string in a function inside of a class, how can i do this without having to copy and paste the entire string over again inside of my class.

If anyone can help me this would be greatly appreciated.
Is it a) a string literal such as the one in the line below,
printf("Hello, World!\n");
or b) a pointer to a C string you need to pass to a function?

For a), you can do this:
#define STR_000 "your string here"
For b), you need to tell the function you want to pass it a C string:
T f(/*anything else*/ char *str /*anything else*/){/*...*/}
no, it is a string like this:
string names[5] = { "bob", "bill", "betty", "joe", "susy" };

In the .cpp file i need to run a function in a .h file which uses the string from above, located in the .cpp file.
I'm sorry if this does not make too much sense.
Which string? That's an array of five. Do you need all five? Do they ever change?
Ahh, I see what you're getting at. In that case you want something like the following:

1
2
3
4
5
void foo(string* string_array,int size)
{
   for (int i=0;i<size;i++)
      cout << string_array[i];
}


Since an array is just a list of pointers, you can simply pass the array as a function argument. Hope it helps!
that is exactly what i need, thanks. One more question, In main when i say blah.foo() what do i put in the parenthesis?
foo(names,5);
When i try to compile it i get some errors, here is my source
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;
#include "test.h"

int main()
{
	int len = 3;
	string test[3] = { "one", "two", "three" };
	hello yea;
	yea.stuff(test,3);	
}


and test.h

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

class hello
{
	public:
	void stuff(string test, int size)
	{
	cout << string test[1];
	}
};
in stuff(), parameter test is not a pointer. You need to make it a pointer in order for this to work.
how do i make it a pointer, can you give me an example/syntax.
You just needed to copy the types g0dwyn gave you...
void stuff(string *test, int size)
Belkdaddy, you might want to take a look at some of the tutorials. Pointers are an invaluable part of C++, and it sounds like you're starting to come around to using them. Check them out and see what you can find.

http://www.cplusplus.com/doc/tutorial/pointers.html
http://www.cplusplus.com/doc/tutorial/
Topic archived. No new replies allowed.