09.06.2012, 07:28
Even though I pretty much feel like I explained the same as MP2 I'd like to add something important!
I hope this prevents issues.
EDIT:
To your last question: sizeof(variablename) returns the cells
Each cell can hold a character/number
so if you have a text like "Hello" you need 5 cells, keep in mind though that in programming everything starts with ZERO.
EDIT2:
To avoid confusion, when I say "cannot use XX" I don't mean you can't reference to the variable like this
The above is FINE, what I mean is you cannot do this:
If you have any open questions feel free to ask!
Regards.
pawn Код:
new variablename[somesize];
// remember to use the VARIABLENAME in sizeof not always sizeof(string) otherwise you WILL get errors at some point.
format(variablename, sizeof(variablename), "Some text with %s specifiers", stuff to replace the specifiers with in order of specifiers);
// Also keep in mind that a variablename may not be used twice in the same segment, a segment is this:
{
// code
}
// This also applies to global variables!
new globalvariable;
// segment
{
// cannot use globalvariable
}
Obviously if there is inner segments
{
new var;
{
// cannot use var
}
}
EDIT:
To your last question: sizeof(variablename) returns the cells
pawn Код:
new variablename[cells];
so if you have a text like "Hello" you need 5 cells, keep in mind though that in programming everything starts with ZERO.
pawn Код:
new hello[5];
format(hello, sizeof(hello)/* returns 5 */, "Hello");
// Now, the above looks like this:
string[0]='H';
string[1]='e';
string[2]='l';
string[3]='l';
string[4]='o';
string[5]='\0'; // this tells the script that the string has ended and is a internal thing, so simply keep in mind that everything starts with zero but don't take it into account when you define the cells! aka: I need 5 cells for hello, 0,1,2,3,4 but since we don't take into account the zero when declaring, we declare 1,2,3,4,5 so 5 cells. There NEEDS to be ONE CELL for the NULL terminator!
EDIT2:
To avoid confusion, when I say "cannot use XX" I don't mean you can't reference to the variable like this
pawn Код:
new var = 2;
{
printf("Debug: %d", var);
}
pawn Код:
new var = 2;
{
new var = 3;
}
// Warning: var shadows a variable at a preceding level
Regards.