Difference between "const text[]" and "text[]"?
#1

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

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

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?
Reply
#4

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
}
Reply
#5

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;
}
Reply
#6

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

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

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

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
}
Reply
#10

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?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)