Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have a function from which I need

Status
Not open for further replies.

lbucko

Programmer
Feb 13, 2002
11
0
0
IE
I have a function from which I need to return an array of values but I'm not sure how to go about this, do I need to use some sort of pointers? Some code shown below:

while (index<num_site)
{
max = get_biggest(index, results);

res_arr[index_res++] = results[max][index[max]-1].score;

}//while

}//m
I need to return res_arr from this, any help appreciated.



 
return res_arr would return it for you... can you repost it with code tags? Also, the whole function would help as well

Matt
 
I thought I wouldn't be able to return an array like that, full code for the functionn is as follows:

Code:
int merge(Res results[][100], int num_sites)
	{
		
		int index[NUM_SITES];		//index array of sites returned
		
		
		int res_arr[500];//overall results array
		const num_site=0;
		int index_res;
		int max=0;
		index_res=0;			

		for (i=0; i<NUM_SITES; i++)
		{
			index[i]=0;
			
		}//for

		while (index[i]<num_site)
		{
			max = get_biggest(index, results);

			res_arr[index_res++] = results[max][index[max]-1].score;
			
		}//while
			
	}//merge

 
Hi,

I think you have some errors in the code you sent.

First of all, num_site is const and == 0. Then, you are initializing index = 0 with that for loop so:

while(index<num_site)
....

will never happen because 0 is never < 0...In fact, let's say NUM_SITES = 10, you are setting index[0-9] = 0 in the for loop, and then trying to access index[10] because i++ will happen before it exits the loop. index[10] doesn't exist. Try displaying i after the for loop to see its value.

Also, if you are altering an array in a called function, you don't need to return it to keep its changes. A simple variable is passed by value and a copy is made which is why you must return any changes to it. An array, though is passed by reference already so the actually array in memory is the one being used.

Hope that helps.

-Tyler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top