What differs these 2 codes?
#1

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

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";
Reply
#3

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";
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)