Wow! Structs are a pretty powerful tool!

You must understand the Game Editor concepts, before post here.

Wow! Structs are a pretty powerful tool!

Postby GEuser » Sat Apr 06, 2013 3:39 am

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).
GEuser
 
Posts: 204
Joined: Thu Jan 05, 2012 3:08 pm
Score: 19 Give a positive score

Re: Wow! Structs are a pretty powerful tool!

Postby Game A Gogo » Fri Apr 19, 2013 5:59 pm

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...
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Wow! Structs are a pretty powerful tool!

Postby skydereign » Fri Apr 19, 2013 7:29 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Wow! Structs are a pretty powerful tool!

Postby GEuser » Fri Apr 19, 2013 8:45 pm

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.
GEuser
 
Posts: 204
Joined: Thu Jan 05, 2012 3:08 pm
Score: 19 Give a positive score

Re: Wow! Structs are a pretty powerful tool!

Postby skydereign » Fri Apr 19, 2013 9:05 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Wow! Structs are a pretty powerful tool!

Postby GEuser » Fri Apr 19, 2013 9:40 pm

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:
GEuser
 
Posts: 204
Joined: Thu Jan 05, 2012 3:08 pm
Score: 19 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest

cron