Page 1 of 1

Wow! Structs are a pretty powerful tool!

PostPosted: Sat Apr 06, 2013 3:39 am
by GEuser
Finally got structs working. Don't know why they weren't working before.

Basically, even if a struct is typedef defined in global code it's initialised global value does not work. Although discovered a powerful technique where if you define a global function to manipilate a global struct you can assign it any value and it works just fine! This is an extremely powerful method for over comming the limitations of the number of parameters that can be used in a function. Wow! this is great!

Image
Image
Image

Getting to grips with Structs was important for me to be-able to do some serious stuff including AI and Neural Networks. Now it's just a matter of being able to fully understand pointers (getting there), the brain pain has been worth it. And now the real pain begins->memory management (Malloc, Calloc, memcpy, memcmp, memmove, memset).

Re: Wow! Structs are a pretty powerful tool!

PostPosted: Fri Apr 19, 2013 5:59 pm
by Game A Gogo
btw, in your function where you have
void fooAssign(foo * tempFoo, int num1, int num2, int num3, char str[30])

you can replace it with
void fooAssign(foo &tempFoo, int num1... etc

if you put a & instead of *, it will grab the refference of the variable, so when you pass it in

instead of doing
fooAssign(&AFoo...

you can simply do
fooAssign(AFoo...

Re: Wow! Structs are a pretty powerful tool!

PostPosted: Fri Apr 19, 2013 7:29 pm
by skydereign
Game A Gogo wrote:btw, in your function where you have
void fooAssign(foo * tempFoo, int num1, int num2, int num3, char str[30])

you can replace it with
void fooAssign(foo &tempFoo, int num1... etc

if you put a & instead of *, it will grab the refference of the variable, so when you pass it in

instead of doing
fooAssign(&AFoo...

you can simply do
fooAssign(AFoo...

You can't use references in C, that's a C++ thing.

Re: Wow! Structs are a pretty powerful tool!

PostPosted: Fri Apr 19, 2013 8:45 pm
by GEuser
skydereign wrote:
Game A Gogo wrote:btw, in your function where you have
void fooAssign(foo * tempFoo, int num1, int num2, int num3, char str[30])

you can replace it with
void fooAssign(foo &tempFoo, int num1... etc

if you put a & instead of *, it will grab the refference of the variable, so when you pass it in

instead of doing
fooAssign(&AFoo...

you can simply do
fooAssign(AFoo...

You can't use references in C, that's a C++ thing.


Am trying to dispel the confusion, this is where using pointers and structs became a nightmare for me to understand in gE because I came from a novice c++ background and assumed you could pass by reference.

To recap then you can't pass by reference per sa or only in the case of structs?

Then when I'm using stuff like myFunction(&myActor); I am not passing by reference but giving up address for receiving function: void myFunction( Actor * myActor) to utilise for the pointers sake only NOT reference as in the C++ sense of reference?

Originally I tried to research all this but no where was it clearly stated that you can't pass by reference (in the C++ sense) in C. No wonder I got confused. Perhaps it might be a good idea to create a thread that clearly states these kinds of differences between C and C++ that seem obvious to experienced c peeps but confusing to novices like me.

Re: Wow! Structs are a pretty powerful tool!

PostPosted: Fri Apr 19, 2013 9:05 pm
by skydereign
Pass by reference in C, is passing via pointers (addresses). C++ adds another way by allowing you to do the ampersand trick (references) that Game A Gogo mentioned. This makes it unnecessary to use the address operator when calling the function. This short cut is not available in C, and therefore what you had is how you are supposed to pass things by reference. But, both methods are using the idea pass by reference.

Re: Wow! Structs are a pretty powerful tool!

PostPosted: Fri Apr 19, 2013 9:40 pm
by GEuser
skydereign wrote:Pass by reference in C, is passing via pointers (addresses). C++ adds another way by allowing you to do the ampersand trick (references) that Game A Gogo mentioned. This makes it unnecessary to use the address operator when calling the function. This short cut is not available in C, and therefore what you had is how you are supposed to pass things by reference. But, both methods are using the idea pass by reference.


Thanks for clarying all that. You know had I been introduced to C first rather then C++ I would have comprehended the likes of pointers and such better.

The baggage that I brought from C++ was a confused association of & for whenever you wanted to READ IN a variable but not permanently change it and * to specifically CHANGE the raw content of whatever was passed in. It's all slowly comming into focus :lol: