SA-MP Forums Archive
Returning a NULL string, Crashes compiler? - 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: Returning a NULL string, Crashes compiler? (/showthread.php?tid=328158)



Returning a NULL string, Crashes compiler? - [HLF]Southclaw - 23.03.2012

Hey, a simple question this time, I've never used this method while coding before so I'm not sure how to workaround this:

Say you have a function that returns 0 if it fails:

Code:
func(something)
{
    if(something == somethingelse) return 5;
    return 0;
}
So I did the same thing with a string:


Code:
func(something)
{
    if(something == somethingelse) return somestring;
    return "";
}
This crashes the compiler and so does this:
Code:
func(something)
{
    if(something == somethingelse) return somestring;
    return "<NULL>";
}
I never knew you couldn't return strings like return "this";




So now I'm at a loss and don't know what to return at the end, I can't return nothing because I get a 'must return something' error.


Re: Returning a NULL string, Crashes compiler? - Jonny5 - 23.03.2012

maybe try return "\0";
i think that's the null char
if that don't work return a 0
and check it with
Code:
if(func(something))
{
    somevar = func(something);
}



Re: Returning a NULL string, Crashes compiler? - [HLF]Southclaw - 23.03.2012

I already said that returning anything like "this" crashes it, that's why "<NULL>" crashed it.
I can't return with 0 because I get a return type mismatch error because one return statement returns an array and the other just returns 0, which is an Integer.


Re: Returning a NULL string, Crashes compiler? - Jonny5 - 23.03.2012

i see what you mean
ima mess with this ill edit this post in a min.



edit:

the work around would be to pass the var byref to the function also.
Code:
func(something,@vartoset);

//instead of 

vartoset = func(something);
set the vars value in the function using strcpy and return 1 for true and 0 for false.

i think this is done with the @ char but dont quote me on this,


or use the code below.
HTH's


Respuesta: Returning a NULL string, Crashes compiler? - admantis - 23.03.2012

i'm not sure you can do that.
however you can define a string, and format it, then you return the symbolic name of that string. that way you can correctly return a string, not directly.
Code:
//for example
new string[4];
format( string, 4, "null" );
return string;

// where ...
return "null";
// shouldn't work



Re: Returning a NULL string, Crashes compiler? - blewert - 23.03.2012

I'm kind of not sure what you want (with the return type), but if you want to return a string value, then yeah the PAWN compiler crashes with string literals. However, it doesn't seem to crash when you return a character array by reference:

Code:
test( )
{
	new szReturn[ 4 ] = { 'h', 'i', '!', '\0' };
	return szReturn;
}
or ..

Code:
test( )
{
	new szReturn[ 4 ] = "hi!";
	return szReturn;
}
Both seem to compile fine for me. I'm not sure if the tag mismatch would be gone, because I don't know what tag you want - I assumed a character array. If you want to return a null string, return '\0' - it's the zero-termination character for strings.

EDIT: Check admantis' code.


Re: Returning a NULL string, Crashes compiler? - MP2 - 23.03.2012

Return a space?


AW: Returning a NULL string, Crashes compiler? - BigETI - 23.03.2012

Make sure that you do return something like
Code:
"\1"
for null strings.


Re: Returning a NULL string, Crashes compiler? - [HLF]Southclaw - 24.03.2012

Thanks for the replies!

I finally went with creating a null temp string and returning that.
I could have used a reference but that means not simply returning the value straight into a function or assignment, it means a new string variable, and that looks a bit more cluttered to me!

Unfortunately I couldn't just create a 1 or 2 cell array as the return sizes had to match, so I had to create a completely useless 256 cell array!

But oh well, problem solved! In case you're wondering it's for an update to my File Script, that will be posted tomorrow after a full bug test (link in my sig)


Re: Returning a NULL string, Crashes compiler? - cessil - 24.03.2012

I don't understand why you'd need to return a string in pawn when you can just pass the string and return 1 or 0 depending on if it was changed or not

Code:
stock something(text[],len)
{
  if(variable)
  {
    format(text,len,"text%d",1);
    return 1;
  }
  return 0;
}

if(something(string,128)) {
  print(string);
}
else
{
  print("failed something");
}