Raised This Month: $12 Target: $400
 3% 

int array to float array from function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Boonie
Member
Join Date: Aug 2015
Old 07-26-2017 , 06:24   int array to float array from function
Reply With Quote #1

I'm trying to convert an int array to a float array but I'm not quite sure how. Currently I have it so "out" gets the array value too, but this is what is making it not compile. If I remove the [i] from out, it will just use the last value from the int.

Code:
void int2float(const int[] array, int count) {
	float out;
	
	for (int i = 0; i < count; i++)
	{
		out[i] = float(array[i]);
	}
	
	return out;
}
Also is there any reason why you cant use sizeof in a for loop like so?

Code:
for (int i = 0; i < sizeof(array); i++)
Edit: Found out why it didn't work, forgot to specify the size of out. Now I get a warning on "return out" with a tag mismatch, yet its not mismatched? Should I just ignore it?

Last edited by Boonie; 07-26-2017 at 06:55.
Boonie is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-26-2017 , 06:54   Re: int array to float array from function
Reply With Quote #2

Have the caller provide the output array, meaning that the prototype should look like
PHP Code:
void int2floatarray(const int[] infloat[] outint count); 
You can't do sizeof(array) in that case because the size of array parameter is not known at compile-time. sizeof is a compile-time operator.

Last edited by klippy; 07-26-2017 at 06:54.
klippy is offline
Boonie
Member
Join Date: Aug 2015
Old 07-26-2017 , 07:09   Re: int array to float array from function
Reply With Quote #3

Quote:
Have the caller provide the output array, meaning that the prototype should look like
What do you mean by "Have the caller provide the output array"?

Quote:
You can't do sizeof(array) in that case because the size of array parameter is not known at compile-time. sizeof is a compile-time operator.
Ah, that makes sense.
Boonie is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 07-26-2017 , 07:15   Re: int array to float array from function
Reply With Quote #4

You can't return arrays, you'll have to pass them in edit them like the example provided above
See: https://forums.alliedmods.net/showthread.php?t=92878

Last edited by hmmmmm; 07-26-2017 at 07:16.
hmmmmm is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 07-26-2017 , 13:50   Re: int array to float array from function
Reply With Quote #5

You can return arrays, as the link hmmmmm provided actually explains. See BAILOPAN's response there.
Fyren is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 07-26-2017 , 17:32   Re: int array to float array from function
Reply With Quote #6

Code:
public void int2float(const int[] array, float[] out, int count)
{
	for (int i = 0; i < count; i++)
	{
		out[i] = float(array[i]);
	}
}
Code:
stock float[] int2float(const int[] array, int count)
{
	float[] out = new float[count];
	for (int i = 0; i < count; i++)
	{
		out[i] = float(array[i]);
	}
	return out;
}
__________________

Last edited by Neuro Toxin; 07-26-2017 at 17:37.
Neuro Toxin is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-26-2017 , 18:13   Re: int array to float array from function
Reply With Quote #7

Quote:
Originally Posted by Neuro Toxin View Post
Code:
stock float[] int2float(const int[] array, int count)
{
	float[] out = new float[count];
	for (int i = 0; i < count; i++)
	{
		out[i] = float(array[i]);
	}
	return out;
}
You can't return a dynamic array. Sizes of destination and output arrays have to be known at compile-time, and the destination array has to be equal or greater in size than the output array.

Last edited by klippy; 07-26-2017 at 18:17.
klippy is offline
Boonie
Member
Join Date: Aug 2015
Old 07-26-2017 , 20:23   Re: int array to float array from function
Reply With Quote #8

Ah I see now, thanks.
Boonie is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:53.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode