|
Call by Reference in C-Script
#58459
11/06/05 13:13
11/06/05 13:13
|
Joined: Oct 2002
Posts: 4,753 Munich, Bavaria, South of Germ...
TripleX
OP
Expert
|
OP
Expert
Joined: Oct 2002
Posts: 4,753
Munich, Bavaria, South of Germ...
|
Hey As you know you can not easily change Variables passed to a function as parameter, in this function. Example: Code:
function test(a_);
function main() { var c = 0; test(c); }
function test(a_) { a_ = 50; }
This won't work. Why? Because we work with a copy of the orginal value not with the variable itself. So we have to pass the pointer of the variable to the function? How does this work? Exactly as if you would pass a vector to a function. With the "&" operator. What does that mean? You always pass the pointer of a vector to a function! That's why the same code as above with pointers would be possible. A code example: Code:
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; //c will have the value 50 }
I posted that because this info is really nowhere mentioned in any tutorial..Via & you don't HAVE to pass a vector! You say the compiler that he has to expect a memory location as parameter. Triple-X PS: That is also the reason why in a DLL you expect a pointer to a vector but the value of a var ( DLLFUNC var tst(var b); DLLFUNC var tst(VECTOR* b); )
|
|
|
Re: Call by Reference in C-Script
[Re: HeelX]
#58462
11/06/05 17:27
11/06/05 17:27
|
Joined: Mar 2005
Posts: 309 Germany, Bavaria
Sinthoras
Senior Member
|
Senior Member
Joined: Mar 2005
Posts: 309
Germany, Bavaria
|
I don't know if it is mentioned in the old manual, but if you download the new release, it is included: Quote:
Note that non-numeric arguments in functions keep their value only until the first wait instruction. Functions also accept numeric arrays or vectors as parameters. Because C-Script makes no difference between variables and arrays, 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 "&".
Example: function vector_add (&sum,&v1,&v2) { // calculate the sum of two vectors sum[0] = v1[0] + v2[0]; sum[1] = v1[1] + v2[1]; sum[2] = v1[2] + v2[2]; }
var vector1[3] = 1,2,3; var vector2[3] = 4,5,6; var vector3[3]; ... vector_add(vector3,vector1,vector2); // vector3 now contains 5,7,9
Okay, it is not shown that you also can take single variables as a parameter, so nevertheless, thanks TripleX, I also didn't know that. Very handy sometimes, i think. Cheers Sinthoras
|
|
|
Re: Call by Reference in C-Script
[Re: Sinthoras]
#58463
11/06/05 17:58
11/06/05 17:58
|
Joined: Oct 2002
Posts: 4,753 Munich, Bavaria, South of Germ...
TripleX
OP
Expert
|
OP
Expert
Joined: Oct 2002
Posts: 4,753
Munich, Bavaria, South of Germ...
|
"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 "&"." So the manual says that this "&" is not possible / needed with vars.. And thats absolutly wrong And it is also wrong that the "&" is only needed to "say" the function that the parameter will be an array.. this is only the most simplest explanation ^^
|
|
|
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.
|
|
|
Re: Call by Reference in C-Script
[Re: TripleX]
#58467
11/11/05 13:45
11/11/05 13:45
|
Joined: Sep 2003
Posts: 9,859
FBL
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 9,859
|
Every var actually IS a three dimensional vector. Always. By defining "var a;" you always have access to a.x, a.y, a.z. So the & operator does work. Even if you use just a single var, because it always is a vector. Ok, you're right, it's not mentioned and should be added. Source of information: jcl and various threads here in the forum
|
|
|
Re: Call by Reference in C-Script
[Re: Calined]
#58468
11/11/05 13:51
11/11/05 13:51
|
Joined: Mar 2005
Posts: 309 Germany, Bavaria
Sinthoras
Senior Member
|
Senior Member
Joined: Mar 2005
Posts: 309
Germany, Bavaria
|
Well, you definately CAN do that ( first example ). But if you want to change a variable by a function, you have to do it with pointers ( & in c-script, second example ). But if you pass zero to the second function which expects a pointer to a variable, the error will occur if the engine tries to refer to the location in the memory, by the pointer information (because the pointer points at "zero", it is an empty pointer).
The great thing about the pointer parameter is that you can change a variable / array within another function. Means, you have to take a variable, not a value, for a parameter in the function:
function test(&a) { a[0] = 2; }
var01 = 1; //var01 is 1 test(var01); //var01 is 2 now
If you would code it without the pointer syntax:
function test(a) { a = 2; }
var01 = 1; //var01 is 1 test(var01); //var01 is still 1
the variable ( var01 ) will stay unchanged.
Sinthoras
@Firoball: Wow, that's a really strange thing.. I didn't know that. C-script is a very unusual language ^^. I tried it, and it really works. I would NEVER have expected that (before you said it). This means, "var" is not a single "long" type, but three?? What a waste of memory
|
|
|
|