SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 23/03/2012, 11:23 PM   #1
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,432
Reputation: 936
Default Returning a NULL string, Crashes compiler?

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:

pawn Code:
func(something)
{
    if(something == somethingelse) return 5;
    return 0;
}

So I did the same thing with a string:


pawn Code:
func(something)
{
    if(something == somethingelse) return somestring;
    return "";
}
This crashes the compiler and so does this:
pawn 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.
[HLF]Southclaw is offline   Reply With Quote
Old 23/03/2012, 11:32 PM   #2
Jonny5
Gangsta
 
Join Date: Mar 2012
Location: Mi, USA
Posts: 938
Reputation: 146
Default Re: Returning a NULL string, Crashes compiler?

maybe try return "\0";
i think that's the null char
if that don't work return a 0
and check it with
pawn Code:
if(func(something))
{
    somevar = func(something);
}
Jonny5 is offline   Reply With Quote
Old 23/03/2012, 11:33 PM   #3
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,432
Reputation: 936
Default Re: Returning a NULL string, Crashes compiler?

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.
[HLF]Southclaw is offline   Reply With Quote
Old 23/03/2012, 11:36 PM   #4
Jonny5
Gangsta
 
Join Date: Mar 2012
Location: Mi, USA
Posts: 938
Reputation: 146
Default Re: Returning a NULL string, Crashes compiler?

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.
pawn 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
Jonny5 is offline   Reply With Quote
Old 23/03/2012, 11:41 PM   #5
admantis
High-roller
 
Join Date: Sep 2009
Posts: 2,264
Reputation: 228
Default Respuesta: Returning a NULL string, Crashes compiler?

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.
pawn Code:
//for example
new string[4];
format( string, 4, "null" );
return string;

// where ...
return "null";
// shouldn't work
admantis is offline   Reply With Quote
Old 23/03/2012, 11:44 PM   #6
blewert
Gangsta
 
blewert's Avatar
 
Join Date: Jan 2008
Location: Wales
Posts: 893
Reputation: 134
Default Re: Returning a NULL string, Crashes compiler?

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:

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

or ..

pawn 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.
__________________
blewert is offline   Reply With Quote
Old 23/03/2012, 11:44 PM   #7
MP2
Godfather
 
MP2's Avatar
 
Join Date: Jan 2011
Location: England
Posts: 6,325
Reputation: 894
Default Re: Returning a NULL string, Crashes compiler?

Return a space?
__________________
>> Click here to see my releases.

Please do not PM me requesting help. Here you have a forum with 11,000+ active users - why are you limiting your help to one person?
MP2 is offline   Reply With Quote
Old 23/03/2012, 11:46 PM   #8
BigETI
Gangsta
 
BigETI's Avatar
 
Join Date: Mar 2010
Location: Germany
Posts: 863
Reputation: 160
Default AW: Returning a NULL string, Crashes compiler?

Make sure that you do return something like
pawn Code:
"\1"
for null strings.
BigETI is offline   Reply With Quote
Old 24/03/2012, 12:37 AM   #9
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,432
Reputation: 936
Default Re: Returning a NULL string, Crashes compiler?

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)
[HLF]Southclaw is offline   Reply With Quote
Old 24/03/2012, 01:05 AM   #10
cessil
Beta Tester
 
cessil's Avatar
 
Join Date: Apr 2009
Posts: 2,302
Reputation: 251
Default Re: Returning a NULL string, Crashes compiler?

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

pawn 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");
}
cessil is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I set null a string? Konstantinos Scripting Help 2 21/02/2012 07:18 PM
Returning string. Roko_foko Help Archive 13 15/07/2011 07:24 PM
Returning a string maramizo Help Archive 5 14/04/2011 07:35 PM
String cuts when returning? ziomal432 Help Archive 0 12/06/2010 07:57 PM


All times are GMT. The time now is 03:37 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.