array as an argument to a function

I am trying to pass an array as an argument to a function, and it does not seem to be working. Any ideas?

This is my function...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int explode(string src, char c, string out[])
{
	int seperatorcount = 0;
	string buffer = "";
	for (int i=0; i<src.length(); i++)
	{
		if (src[i] == c)
			seperatorcount++;
	}

	out = new string[seperatorcount+1];
	int explosions = 0;
	for (int i=0; i<src.length(); i++)
	{
		if (src[i] == c)
		{
			out[explosions++] = buffer;
			buffer = "";
		}
		else
		{
			buffer += src[i];
		}
	}
	
	out[explosions++] = buffer;
	return explosions;
}


And then later on...
1
2
3
string *filters_a;
int nfilters = explode(filters, '<', filters_a);
cout << filters_a[i] << endl;


And then it crashes. I'm banging my head against the wall here, Any idea where I went wrong?
Last edited on
out[explosions++] = buffer; I don't suspect that this would work. I should think it would need to be
1
2
explosions++;
out[explosions] = buffer;
No, that part works fine, in fact from within the function, everything appears to be perfect, and I can even check the values in the array. But when the function returns, the address of filters_a has not even changed!
Anyone?
you need to pass the string filters_a by reference.

int explode(string src, char c, string out[]) // -> passing by value

int explode(string src, char c, string & out[]) // -> passing by reference

P.s : unless you are using array of strings, you really don't need to say

string out[] // in your parameter

string out // without [] would be fine

also I would suggest that you always use passing by reference when passing strings, and if you don't want to change the data in your string you would pass it with 'const'.

Edit: I'm sorry, what exactly are you trying to do?

I hope this help,
-TAZO
Last edited on
I'm using an array of strings, not a string. Passing arrays like I did passes a pointer to the first array element (which is by reference).
Trying to figure this out, is your program not compiling or is it just not functioning properly
int explode(string src, char c, string * out) // try this

edit: "string out[]" meaning you have an array of strings ... where its only a pointer to a string pointing to nothing yet till its goes into the function.
Last edited on
My program does compile, and explode works. From inside explode I can check the array elements, and all is fine. Once the function returns, accessing any element of the array crashes. And I have tried the following as arguments:

*out
*out[]
out[]

Thanks again for the help, by the way
Last edited on
The problem is here:

out = new string[separatorcount+1];

You are changing out to point to a new address. When you pass in filters_a, it continues to point to the old address which is not modified by the function.

To fix the problem, just change the function signature to this:

int explode(string src, char c, string*& out)

By passing out by reference, the function will modify filters_a.
My Hero!

I knew it had to be something simple. Thanks a bunch :D
Topic archived. No new replies allowed.