Creating array with size declared in variable -
GiS - 04.05.2013
Hey,
I know it's possible, what am I doing wrong here:
pawn Код:
new size = 5,
arr[size];
// error 008: must be a constant expression; assumed zero
Re: Creating array with size declared in variable -
RajatPawar - 04.05.2013
EDIT: My bad.
AW: Creating array with size declared in variable -
Nero_3D - 04.05.2013
The expression must be constant
pawn Код:
const size = 5;
new array[size];
Re: Creating array with size declared in variable -
GiS - 04.05.2013
Nope, that's not the solution. Same errors given.
Quote:
Originally Posted by Nero_3D
The expression must be constant
pawn Код:
const size = 5; new array[size];
|
Works, thanks.
But different attempt now:
pawn Код:
const size = getConstant(); // Returns any positive integer
new array[size];
This does not work.
AW: Creating array with size declared in variable -
Nero_3D - 04.05.2013
Sure it doesn't, if you want to achieve something like an dynamic array than I will disappoint you that's not possible
Re: Creating array with size declared in variable -
Pottus - 04.05.2013
Use #define MY_ARRAY_SIZE 5
then new array[MY_ARRAY_SIZE];
Re: AW: Creating array with size declared in variable -
GiS - 04.05.2013
Quote:
Originally Posted by Nero_3D
Sure it doesn't, if you want to achieve something like an dynamic array than I will disappoint you that's not possible
|
A dynamic array would be a vector, however, that's not what I am trying to achieve. I am trying to get the size for the array I need and then create this array with the required size.
Quote:
Originally Posted by [uL]Pottus
Use #define MY_ARRAY_SIZE 5
then new array[MY_ARRAY_SIZE];
|
That won't solve the problem as I cannot define the returned integer of a function.
Re: Creating array with size declared in variable -
Pottus - 04.05.2013
You can't use dynamic arrays I thought that was mentioned already? You can't define an array some arbitrary size it must be a constant when compiling. What you could do is make your array larger than what you need and use your function to define the upper bound. There is one other option that is probably less than ideal
https://sampforum.blast.hk/showthread.php?tid=58827
Re: Creating array with size declared in variable -
RajatPawar - 04.05.2013
You can do somethings with 'new' and 'delete' operators, defining them as constants, then putting it as the size, then checking with code IF it's an okay size, if not, deleting it and redefining it, and so on.