Assigning strings to a global variable
#1

So, I'm assigning strings to a global variable and I was wondering this:

Why would this work:

pawn Код:
new Msg[][MAX_PLAYERS];
But why do you need to change this:

pawn Код:
new Msg[][MAX_PLAYERS][5];
to this:

pawn Код:
new Msg[MAX_PLAYERS][5][128] // 128 for string length.
I'm trying to understand it but I don't really get it, and I really wanna know.

Thank you in advance.
Reply
#2

If you want a string for every player, you should do this:

new Msg[MAX_PLAYERS][string_length];

AFAIK, Only literal constant strings can have an unspecified size ('[]') i.e.:

new Msg[] = "Hello"; // Will be 6 cells (5 letters + null terminator).
Reply
#3

I see, but then if you use:

pawn Код:
new Msg[MAX_PLAYERS][5];
Why can you do:

pawn Код:
Msg[playerid][0] = blabla;
Msg[playerid][1] = blabla;
This really confuses me.

Also:

this worked fine for me

pawn Код:
new Msg[][MAX_PLAYERS];
while this didn't work when I tried it:

pawn Код:
new Msg[MAX_PLAYERS][128]; // 128 for strin length.
Reply
#4

pawn Код:
new Msg[MAX_PLAYERS][5];
Creates 2d array, MAX_PLAYERS wide, 5 cells high. You can do whatever you want within the boundaries.

pawn Код:
new Msg[MAX_PLAYERS][5][128];
Creates 3d object, MAX_PLAYERS wide, 5 cells long, and 128 cells high. So it takes MAX_PLAYERS * 5 * 128 cells (you can store total MAX_PLAYERS * 5 127 char long strings. Also, a awful lot of heap).

The unspecified size is magic for me, and I don't have pawn-lang.pdf here, but I think MP2 is right, and it should be only used for constant strings

#edit:

pawn Код:
new Msg[][MAX_PLAYERS];
If this syntax were correct, then you would create array of undefined number of strings, with max char length of MAX_PLAYERS - 1
Reply
#5

You probably received an error about the array sizes needing to match. MP2 described it as a literal constant string, doing this:

pawn Код:
new msg[] = "NULL";
In that case, the compiler will only assign 5 cells to that variable. Whereas when you do this:

pawn Код:
new msg[MAX_PLAYERS][128] = "NULL";
... the compiler doesn't know how long msg is at the time you do = "NULL".

Make a little sense, at all?
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)