SA-MP Forums Archive
Difference between "const text[]" and "text[]"? - 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: Difference between "const text[]" and "text[]"? (/showthread.php?tid=368737)



Difference between "const text[]" and "text[]"? - [DOG]irinel1996 - 14.08.2012

Hi,
I have a little doubt between two things:
pawn Код:
stock MyFunc(playerid, const msg[]) //This one

stock MyFunc(playerid, msg[]) //And this
What's exatly the difference?
I know that const is for constant variables, that doesn't change their values.
So, what's its point there, in a stock?

Thank you!


Re: Difference between "const text[]" and "text[]"? - MicroD - 14.08.2012

Prevent arrays from being modified, with that const.

Код:
stock fail(const msg[])
{
    msg[0] = '0';
}
It will not be changed.

And it will cause error and warning:
Код:
error 022: must be lvalue (non-constant)
warning 215: expression has no effect



Re: Difference between "const text[]" and "text[]"? - IceMeteor - 14.08.2012

Quote:
Originally Posted by MicroD
Посмотреть сообщение
Prevent arrays from being modified, with that const.

Код:
stock fail(const msg[])
{
    msg[0] = 0;
}
It will not be changed.
You mean changes like what?


Re: Difference between "const text[]" and "text[]"? - MicroD - 14.08.2012

Look:

Код:
main()
{
    fail("trol");
    win("trol");
}

stock fail(const msg[])
{
    msg[0] = 'r';
    print(msg); //Can not be printed because of error
}
ERROR

stock win(msg[])
{
    msg[0] = 'r';
    print(msg);//Will print rrol
}



Re: Difference between "const text[]" and "text[]"? - [DOG]irinel1996 - 14.08.2012

So, there isn't a huge difference, because if we do something as that:
pawn Код:
stock SendMsg(playerid, const msg[])
{
    new info[128];
    format(info,128,"INFO: %s",msg);
    SendClientMessage(playerid,-1,info);
    return 1;
}
//I think it's the same as this:
stock SendMsg(playerid, msg[])
{
    new info[128];
    format(info,128,"INFO: %s",msg);
    SendClientMessage(playerid,-1,info);
    return 1;
}



Re: Difference between "const text[]" and "text[]"? - MicroD - 14.08.2012

Yes, it is almost same. Only difference is that const.
It just prevent msg from being modified.


Re: Difference between "const text[]" and "text[]"? - SEnergy - 14.08.2012

Quote:
Originally Posted by irinel1996
Посмотреть сообщение
So, there isn't a huge difference, because if we do something as that:
pawn Код:
stock SendMsg(playerid, const msg[])
{
    new info[128];
    format(info,128,"INFO: %s",msg);
    SendClientMessage(playerid,-1,info);
    return 1;
}
//I think it's the same as this:
stock SendMsg(playerid, msg[])
{
    new info[128];
    format(info,128,"INFO: %s",msg);
    SendClientMessage(playerid,-1,info);
    return 1;
}
except that you CAN'T change value of "msg" string


Re: Difference between "const text[]" and "text[]"? - [DOG]irinel1996 - 14.08.2012

OK guys, got it now.
Thank you!
Also, +


Re: Difference between "const text[]" and "text[]"? - Sinner - 14.08.2012

const(ant) is used when a certain parameter shoulden't be changed. Let's say a function uses a password, would you like that function to change the password while it's doing it's thing? No you woulden't, and to make sure you don't make a mistakes and accidentally change it you may use const.

pawn Код:
// You don't want this function to change the password
// That would be dramatic (just an example)
stock DoSomethingWithPassword(const password[]) {
    // Whatever this function does, it could never change the value of password[] since that value is now a constant
}



Re: Difference between "const text[]" and "text[]"? - [DOG]irinel1996 - 14.08.2012

Quote:
Originally Posted by Sinner
Посмотреть сообщение
const(ant) is used when a certain parameter shoulden't be changed. Let's say a function uses a password, would you like that function to change the password while it's doing it's thing? No you woulden't, and to make sure you don't make a mistakes and accidentally change it you may use const.

pawn Код:
// You don't want this function to change the password
// That would be dramatic (just an example)
stock DoSomethingWithPassword(const password[]) {
    // Whatever this function does, it could never change the value of password[] since that value is now a constant
}
Changed how and by who? By mistake?