Raised This Month: $51 Target: $400
 12% 

Subroutine - Working with Strings


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
surfhope
Member
Join Date: Aug 2010
Location: Austria
Old 02-22-2012 , 02:07   Subroutine - Working with Strings
Reply With Quote #1

Good afternoon guys,

I would like to know how I can work with some strings and a Subroutine. A
Subroutine which returns a string.

I have here one example, how I would do it.

The call:
PHP Code:
new String:query3[200];     
Format(query3sizeof(query3), "SELECT ban_reason FROM smx_bans WHERE player_id = '%s' OR player_ip = '%s'"authip); 
new 
len sizeof(query3);
new 
String:banreason[200]; 
                    
banreason queryToSting(dbquery3[len]); 
The subroutine:

PHP Code:
String:queryToSting(Handle:db, const String:query[])
{
    new 
String:value[200];
    new 
Handle:place;
    
Format(valuesizeof(value), "wrong");
    
    if((
place SQL_Query(dbquery)) != INVALID_HANDLE)
    {
    
        if (
SQL_FetchRow(place))
        {
            
SQL_FetchString(place0valuesizeof(value));
        }

    }

    
CloseHandle(place);
    return 
value;

Please tell me what's wrong/right and what I have to remove/add.

Thanks in advance
Hope
__________________
surfhope is offline
Send a message via Skype™ to surfhope
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 02-22-2012 , 16:49   Re: Subroutine - Working with Strings
Reply With Quote #2

What the hell is a subroutine ?

You can't return a string buffer, since the buffer you allocate in a function is only valid IN the function, when it returns the allocation will be freed.

That's why you have to allocate the buffer outside the function before you call it, and pass it in:

PHP Code:
// Allocate buffer & call function
decl String:banReason[200];
queryToSting(dbquery3banReason);

// Function
String:queryToSting(Handle:db, const String:query[], String:value[])
{
    
// code 
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
surfhope
Member
Join Date: Aug 2010
Location: Austria
Old 02-22-2012 , 18:06   Re: Subroutine - Working with Strings
Reply With Quote #3

You find the explanation here:
http://en.wikipedia.org/wiki/Subroutine
;)

I normally program in c++ and there's called so

Anyway, big thanks for your help, answered my question
__________________

Last edited by surfhope; 02-22-2012 at 18:08.
surfhope is offline
Send a message via Skype™ to surfhope
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 02-23-2012 , 16:45   Re: Subroutine - Working with Strings
Reply With Quote #4

Quote:
Originally Posted by surfhope View Post
You find the explanation here:
http://en.wikipedia.org/wiki/Subroutine
;)

I normally program in c++ and there's called so

Anyway, big thanks for your help, answered my question
A always call it a function in C++ never, never saw anyone calling it a subroutine in C++...
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 05:43.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode