SA-MP Forums Archive
What differs these 2 codes? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: What differs these 2 codes? (/showthread.php?tid=320974)



What differs these 2 codes? - iTorran - 25.02.2012

pawn Код:
new test1[] = "Hello";
print(test1);
   
new test2[6];
format(test2, sizeof test2, "Hello");
print(test2);
I always use the second one as i have no clue what the first one does.
So basically what i'm looking for is an explanation about it


Re: What differs these 2 codes? - Vince - 25.02.2012

Using format to copy strings is horrible. Also that second method would probably give a runtime error, as you're trying to format a string into a destination that is too small too hold the entire string. Whereas if you'd do:
pawn Код:
new test2[5] = "Hello";
The compiler would throw an error: error 018: initialization data exceeds declared size

The first method just assigns the string "Hello" to the array called test1. At compile time, the compiler will calculate the size of this array and it will be compiled as:
pawn Код:
new test1[6] = "Hello";



Re: What differs these 2 codes? - Gh05t_ - 25.02.2012

Also, take note that in the first example, direct assignment IS needed when you do not specify a size of the array.

pawn Код:
new test[];
test = "Hello";