Creating array with size declared in variable
#1

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
Reply
#2

EDIT: My bad.
Reply
#3

The expression must be constant
pawn Код:
const size = 5;
new array[size];
Reply
#4

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.
Reply
#5

Sure it doesn't, if you want to achieve something like an dynamic array than I will disappoint you that's not possible
Reply
#6

Use #define MY_ARRAY_SIZE 5
then new array[MY_ARRAY_SIZE];
Reply
#7

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.
Reply
#8

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
Reply
#9

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)