References?

Posted By: FakeTruth

References? - 10/01/09 17:16

Hello everybody, I have a question:
How do references work in A7 C-Lite?

I want to create something similar to this:
(In C++ the &[ampersand] sign does this)
Code:
function Add (int& result, int a, int b)
{
	result = a + b;
}

// You can call the function like this
int myResult = 0;
Add(myResult, 5, 7);
// myResult is now 12



What would this function look like in C-Lite?

Many thanks.
Posted By: DJBMASTER

Re: References? - 10/01/09 17:33

Code:
function Add (int* myResult, int a, int b)
{
	*myResult = a + b;
}

int myResult = 0;
Add(&myResult, 5, 7);



The reference operator isn't actually needed because lite-c can detect if a function needs a variable or a pointer to the variable.
Posted By: FakeTruth

Re: References? - 10/01/09 19:25

Thanks DJBMASTER!

But now I have another question.
This doesn't work when sending a scalar of a vector as a parameter:
Code:
VECTOR myVector;
Add(&(myVector.x), 5, 7);


No matter where I put the & sign, I always get the memory address of the vector, and not of the scalar x

Could anybody help me with this?
Thanks!
Posted By: Saturnus

Re: References? - 10/01/09 19:38

Hello!

As 'x' is the first member of the VECTOR struct, the address of myVector and its 'x' member are identical.

Therefore
var value = *((var *)&myVector);
is the same as
var value = myVector.x;

Does this answer your question?
Posted By: DJBMASTER

Re: References? - 10/01/09 19:41

I think structs are always passed by a pointer, so you have to take the address of the whole struct, and manipulate it on the other side of the function rather than passing individual members.

Posted By: FakeTruth

Re: References? - 10/01/09 20:16

Thanks everybody, I have got it to work!

In the function I first tried float* instead of int* because the scalars in the vector are floats (I think), but this did really weird stuff. So I changed it to var* and this worked perfectly! I can send each scalar along separately grin

Thanks again guys!

PS: If you know anything about the float* weirdness, could you please tell me what that's about?
Posted By: DJBMASTER

Re: References? - 10/01/09 20:34

Found this statement in the manual...

"Lite-C additionally supports standard C/C++ integer or floating point variables for cases where you need a higher or smaller range, or better precision. The functions described in this section however use only var as argument and return values."

I think the members of the Vector struct are in-fact vars, which in turn can be a int/float.
Maybe that's why only var will work?
Posted By: atari98

Re: References? - 10/03/09 10:32

Maybe itīs an Error
Posted By: EvilSOB

Re: References? - 10/03/09 12:05

Your right DJBMaster, vectors are by nature a set of three vars.
But the manual is 'blurry' because some functions are apparently "overloaded" to be capable of accepting
a> integer-vectors (never seen one of these though...)
b> standard-vectors (which are just three vars in a row in memory)
c> float-vectors (Ive never seen one used, but look at "_vec()" in the manual)

For a better idea of how the different game structures are built,
try taking a look at the .../GStudio7/includes/aTypes.H file.
JUST DONT ACCIDENTLY CHANGE ANY OF IT! or its re-install GameStudio time....
This will give you a good look at the "guts" of many game object structures.

Have fun...
© 2023 lite-C Forums