|
2 registered members (AndrewAMD, TipmyPip),
16,005
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Call by Reference in C-Script
[Re: TripleX]
#58465
11/06/05 20:41
11/06/05 20:41
|
Joined: Mar 2005
Posts: 309 Germany, Bavaria
Sinthoras
Senior Member
|
Senior Member
Joined: Mar 2005
Posts: 309
Germany, Bavaria
|
Antwort auf:
we have to indicate to the function that a parameter should be handled as an array, instead of a single number. For this, the parameter name in the function definition is prefixed by a "&".
hmm.. should be handled as is not absolutely the same as not possible / needed. You could say, a "single variable" is also an array, with the length of one (means also no ending objekt "\0"). And it is definately handled as one, if "handle" means in this case "the same code syntax as normal arrays in c-script".
example: Code:
var array[10]; //array
function test(&a_); //pass the memory location, not a copy of the var
function main() { var c = 0; test(c); }
function test(&a_) { a_[0] = 50; //dereference pointer operator (*) is not needed in c-script array[0] = 50; //<- the same syntax here }
Sure, you are right its only a better explanation for newbs, by saying "the & says the function that the parameter is an array".. explaining pointers (with dereference, memory addresses, initializations, type casting ect) is not needed in most cases, because c-script simply doesnt support much of these things. (it has a special intern system for that, i think you know what i mean ) It is definately not a good thing to forget to write in the manual the possibility to pass variable-pointers too. I just wanted to show that the pointer-parameters are not ABSOLUTELY fogotten .. only not "all" is included *defending the manual with words* Sinthoras
PS: if someone is interested in more information about pointers, here you go: cplusplus.com
--EDIT-- oh, how funny  I found the explanation for the variable pointer passing in the manual, it was just some lines down my old posts quote:
Antwort auf:
Because each variable is also an array at least of length 1, you can pass a variable either as a number or as an array parameter. However there are some subtle differences between the two types of parameters. If you pass a variable to a function that expects a number, it's content is handed over. If you pass the same variable to a function that expects an array parameter, a pointer to the variable is handed over instead. If the function modifies the parameter, modifying a number is 'locally', i.e. happens only internally within the scope of the function. The passed variable itself is not changed. This is not the case for array or vector parameters: Elements of a passed array are modified 'globally'. It is important to understand this difference. Some examples: Code:
function double_vec(&num) { num[0] *= 2; // num[0] is doubled globally //num *= 2; // not allowed, array parameters need an index } ... temp = 1; double_vec(temp); // now temp is globally changed to 2;
uff, this post is getting very long.. ill stop now i think  --/EDIT--
Last edited by Sinthoras; 11/06/05 20:51.
|
|
|
|
|
|