Array return in a function

Hi, i have a function in which it takes in an array, its length, and the length of the new array.

when i run it, the code stops working and aborts-so its not even telling me whats wrong and ive tried a few things to no avail.

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float* avgComparisonsArray(string wordArr[], int max, int num4){//takes parameter max number of letters in a word from the file.
	float* final=NULL;
	
	for(int i=0;i<max;i++){//looping through number of letters
		string* temp=NULL;
		int temp_point=0;//will, at the final, have the total number of words with ith letters in them
		for(int j=0;j<num4;j++){//looping through number of words in list
			if(wordArr[j].length() == i){
				temp[temp_point]=wordArr[j];
				temp_point++;
			}
		}
		//SET ALL FOUND VARS
		//take array and add
		final[i]=avgComparisons(temp,temp_point);
	}
	return final;
}


i can verify that avgComparisons works fine with any other input when run.

when calling it in main im using:

1
2
float *avg_comps=avgComparisonsArray(words,max_length,numwords);
cout << "average comparisons-array " << avg_comps << endl;


words is a pointer to an array of words, max_length is the maximum number of letters in a word in 'words' and numwords is the number of words in 'words'.

it crashes before i see anything printed about 'average comparisons-array' so i dont even know whats happening. im using visual studio2010 express.

i basically just want to re-use the temp array...

any help is much appreciated, thanks.
Last edited on
Briefly looking at your code I see a severe error.

1
2
3
4
5
		string* temp=NULL;
		int temp_point=0;//will, at the final, have the total number of words with ith letters in them
		for(int j=0;j<num4;j++){//looping through number of words in list
			if(wordArr[j].length() == i){
				temp[temp_point]=wordArr[j];

You defined a pointer that was initialized by NULL ( string* temp=NULL; ) and then you tried to assign it a string (temp[temp_point]=wordArr[j];). The program shall abort,
thanks, ive even tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int i=0;i<max;i++){//looping through number of letters
		string* temp=new string[];	
		int temp_point=0;//will, at the final, have the total number of words with ith letters in them
		for(int j=0;j<num4;j++){//looping through number of words in list
			if(wordArr[j].length() == i){
				temp[temp_point]=wordArr[j];
				temp_point++;
			}
		}
		//SET ALL FOUND VARS
		//take array and add
		final[i]=avgComparisons(temp,temp_point);
		delete [] temp;
	}


but it still does the same thing. i just need to re-use the array, but im not sure how to go about it especially since i cant initialize the size because i dont know (from the start) how many words in the list will have (for example) 4 letters or 8 letters in them until its done searching through them all.
Last edited on
This code

string* temp=new string[];
also incorrect.

I can advice to use std::vector<std::string> instead of arrray.
thanks, we're just supposed to used arrays though-i ended up making a function where it didnt return an array, seems to be fine now :)
Topic archived. No new replies allowed.