Enums and Arrays -
Th3Angel - 14.02.2011
I created an Enum and some variables are arrays. When I try to reset these variables to "", I get an error.
pawn Код:
enum playerInfo
{
var1[16],
var2[145],
var3[20];
}
new pInfo[MAX_PLAYERS][playerInfo];
//In some callback
pInfo[playerid][var1] = ""; //line 1093
pInfo[playerid][var2] = ""; //line 1094
pInfo[playerid][var3] = ""; //line 1095
pawn Код:
(1093) : error 047: array sizes do not match, or destination array is too small
(1094) : error 047: array sizes do not match, or destination array is too small
(1095) : error 047: array sizes do not match, or destination array is too small
Re: Enums and Arrays -
Mauzen - 14.02.2011
This is a pawn entity
Use strcat instead,
https://sampwiki.blast.hk/wiki/Strcat
pawn Код:
strcat(pInfo[playerid][var1], "");
Or: (dont know if it works, just an idea)
pawn Код:
pInfo[playerid][var1][0] = EOF;
//or
pInfo[playerid][var1][1] = EOF;
If you got some time, feel free to test it, would be interesting to know if it works, as this would be much faster than strcat.
Basically, EOF is a symbol that tells pawn that the string is over here. Every string has this at the end, this is why
new txt[32];
could save a maximum of 31 chars.
Re: Enums and Arrays -
Th3Angel - 14.02.2011
Quote:
Originally Posted by Mauzen
This is a pawn entity
Use strcat instead, https://sampwiki.blast.hk/wiki/Strcat
pawn Код:
strcat(pInfo[playerid][var1], "");
Or: (dont know if it works, just an idea)
pawn Код:
pInfo[playerid][var1][0] = EOF; //or pInfo[playerid][var1][1] = EOF;
If you got some time, feel free to test it, would be interesting to know if it works, as this would be much faster than strcat.
Basically, EOF is a symbol that tells pawn that the string is over here. Every string has this at the end, this is why
new txt[32];
could save a maximum of 31 chars.
|
Wouldn't strcat just join a variable with ""?
Like, pInfo[playerid][var1] = "Hello";
Wouldn't strcat just make it "Hello "?
As for EOF, it turns the first character of the string into a я.
Re: Enums and Arrays -
PowerPC603 - 14.02.2011
pawn Код:
pInfo[playerid][var1] = "test";
Even this gives me: error 047: array sizes do not match, or destination array is too small.
To store a string, I always use format:
pawn Код:
format(pInfo[playerid][var1], sizeof(pInfo[playerid][var1]), "test");
If you would print this string using:
pawn Код:
print(pInfo[playerid][var1]);
This would print: "test" (without the ").
To clear a string, I use 0:
pawn Код:
pInfo[playerid][var1] = 0;
Printing it again would print: "<null>" (without the ").
Re: Enums and Arrays -
Th3Angel - 14.02.2011
Thanks Mauzen and PowerPC603.
I still wonder though, why I can't use pInfo[playerid][var1] = ""; when I can do it on this variable.
pawn Код:
new msg[128];
msg = "Hello";
msg = "";
Re: Enums and Arrays -
Mauzen - 14.02.2011
Quote:
Originally Posted by Th3Angel
Wouldn't strcat just join a variable with ""?
Like, pInfo[playerid][var1] = "Hello";
Wouldn't strcat just make it "Hello "?
As for EOF, it turns the first character of the string into a я.
|
Oh ok, I will have to read some things again then.
@PowerPC603: Hm, never tried it with a 0
The problem why you cant use it like this:
pawn Код:
new msg[128];
msg = "Hello";
msg = "";
is caused by the way pawn handles strings.
A String is just a integer-array, with one int for each character. And if you use the normal '=' operator, pawn tries to write a string (array, e.g. "Hello", length 5) into another array (msg[128]). But as you can see, these both array do not have the same size, so you cant just set the one=the other. This gives you the error then.
Re: Enums and Arrays -
Th3Angel - 14.02.2011
Quote:
Originally Posted by Mauzen
Oh ok, I will have to read some things again then.
@PowerPC603: Hm, never tried it with a 0
The problem why you cant use it like this:
pawn Код:
new msg[128]; msg = "Hello"; msg = "";
is caused by the way pawn handles strings.
A String is just a integer-array, with one int for each character. And if you use the normal '=' operator, pawn tries to write a string (array, e.g. "Hello", length 5) into another array (msg[128]). But as you can see, these both array do not have the same size, so you cant just set the one=the other. This gives you the error then.
|
Actually you can use it like that. I've used it like that many times and it works and compiles fine with no errors. But I get what you're saying.