| closedArray by reference |
| Started by h0_noMan on 04-01-2010, 11:50 PM |
Threads: 11
Joined: Dec 2007
Hi guys,rnrnI am working on a tricky plugin and i need to send to a function an array by reference. I tried many thins but never succeed, i always end on an error saying that a parameter cant be both an array and a reference.rnrnThanks for help.rnrnExemple :rn
// Just a Functionrnpublic doFunctionA()rn{rn // Array instancern new myArray[32];rn rn // Calling doFunctionB() with myArrayrn doFunctionB(myArray);rn}rnrn// I want doFunctionB() to modifyrn// the previously created arrayrn// declared in doFunctionA()rn//rn// I dont want doFunctionB to creatern// a clone of myArrayrnpublic doFunctionB(&refArray[])rn{rn format(refArray, 31 "It works %s", "xD");rn}Threads: 24
Joined: Oct 2006
Once you leave doFunctionA, myArray[] no longer exists. Thus doFunctionB can't access it. Define myArray[32] globally to access it outside of doFunctionA.
Threads: 11
Joined: Dec 2007
myArray[] still exist until doFunctionA() ends, but when i call doFunctionB(myArray), doFunctionA() wait for doFunctionB() to finish to continue its own instructions, so myArray[] is not destroyed when doFunctionA() call doFunctionB().rnrnHere is the proof that my code works with an integer :rnrn
// Just a Functionrnpublic doFunctionA()rn{rn // Integer instancern new myInteger = 0; rnrn // Calling doFunctionB() with myArrayrn doFunctionB(myArray);rnrn // Will display 100 and not 0rn log_amx("Value of myInteger : %d", myInteger)rn}rnrn// doFunctionB() actually modifyrn// the previously created integer myIntegerrn// and change his value to 100rnpublic doFunctionB(&value)rn{rn value = 100;rn}rnrnI just want to know how to do it with an array.rnrnThanks.Threads: 75
Joined: Nov 2007
#include <amxmodx>rn#include <amxmisc>rnrn#define PLUGIN "New Plug-In"rn#define VERSION "1.0 by Dr.G"rn#define AUTHOR "AMXX DoD Team"rnrnrnpublic plugin_init() rn{rn register_plugin(PLUGIN, VERSION, AUTHOR)rn rn register_clcmd("say !ding", "GetName")rn}rnpublic GetName(id)rn{rn new iName[32]rn get_user_name(id, iName, sizeof iName - 1)rn rn PassNameToPrint(id, iName)rn rn returnrn}rnpublic PassNameToPrint(const id/*int*/, const ClientName[]/*string*/)rn{rn if(id)rn {rn client_print(id, 3, "My Name is : %s", ClientName)rn returnrn }rn rn elsern client_print(0, 3, "My Name is : %s", ClientName)rn rn return rn}Threads: 11
Joined: Dec 2007
That is not what i want.rnrnYou just send an array as parameter.rnrnYour function PassNameToPrint() create a new array (ClientName[]) and put the content of iName[32] into it.rnI dont want to create a new array but to use the previously created array.rnrnI want to send a pointer to an array (or a reference) as a parameter, NOT a simple array.rnMy doFunctionB() will modify the array created in doFunctionA() without creating a new array because it use the pointer and can modify it.rnrnTake a look at my exemple code, the & means reference.rnrnEDIT : I found how to do it.rnrn
public doFunctionA()rn{rn new message[64]rn doFunctionB(message, 63)rnrn log_amx("Message : %s", message)rn}rnrn// This function will change the value of message[]rn// which was created in doFunctionA()rnpublic doFunctionB(data[0], size)rn{rn format(data, size, "Pi : %d.%d", 3, 14159)rn}rnThreads: 3
Joined: Aug 2007
All arrays are passed by reference by default. In fact, I don't think you can do it any other way.nnIn your function you only need to donn
function(arrayname[], size)
