How to make a function that returns a string
#1

Hello,

I am working on a property script, and have made a function, but it does not compile,

I am trying to make it so that it returns "Nobody"

pawn Код:
stock GetPropertyOwnerName(property[])
{
    new file[256],name[24];
   
    format(file,sizeof(file),"gamemode/config/properties/%s.txt",property);
    if(strcmp(dini_Get(file,"Property_Owner"),"AnUnusableSA-MPName"))
    {
        new str[7] = "Nobody";
        return str;
    }
    format(name,sizeof(name),"%s",dini_Get(file,"Property_Owner"));
    return name;//line 235
}
This is the error I am receiving:
Код:
(235) : error 047: array sizes do not match, or destination array is too small
although if i delete "return str" the error goes away.

It is really doing my head in, if anyone could help i would appreciate it.
Reply
#2

Not sure, but perhaps every string has to be the same size. First of all name should be 25 cells because a name can be 24 characters but there's also the null character. Try this:

pawn Код:
stock GetPropertyOwnerName(property[])
{
    new file[256];
    new name[25] = "Nobody";
   
    format(file, sizeof(file), "gamemode/config/properties/%s.txt", property);
    if(strcmp(dini_Get(file, "Property_Owner"), "~") != 0)
    {
        format(name, sizeof(name), "%s", dini_Get(file, "Property_Owner"));
    }
    return name;
}
I would personally set the name to ~ or something if nobody uses the house (you can't have 1-character names).
Also, put spaces after commas it looks a hell of a lot clearer to read, trust me. Just compare my code and yours.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)