Struct?

Non-platform specific questions.

Re: Struct?

Postby GEuser » Thu Oct 25, 2012 1:17 pm

Thanks sky but I've tried all sorts of versions of struct from online research, including example above, no joy thou :(
I've even tried including it as part of a function rather adding as a alone struct. keep getting these errors:

Error line 3: Expected ;
Error line 4: Expected ;

Code used:-
Code: Select all
struct person
{
    int Age = 0;
};

I'll ask on the Linux Subforum
GEuser
 
Posts: 204
Joined: Thu Jan 05, 2012 3:08 pm
Score: 19 Give a positive score

Re: Struct?

Postby Hblade » Thu Oct 25, 2012 6:22 pm

yeah and GE crashes when using this, if you click "Add" After adding () in front of person
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Struct?

Postby skydereign » Thu Oct 25, 2012 8:17 pm

If you can, try to keep the same questions on the same topic, since there is only one answer. It had nothing to do with linux, just that you can't set initial values for variables within structs. You'd have to create a function for that.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Struct?

Postby SuperSonic » Sat Oct 27, 2012 11:04 pm

Yeah, so for example:
Code: Select all
struct person
{
    int Age = 0;
}
is illegal, but...
Code: Select all
struct person
{
    int Age;
}

void setAge(struct person PersonStruct, int x)
{
    PersonStruct.Age = x;
}
is perfectly fine :)
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Struct?

Postby skydereign » Sat Oct 27, 2012 11:12 pm

That function won't work properly though. Using that method the function will create a local version of the PersonStruct variable (meaning it will only change the value within the function). You'd have to use a pointer for that.
Code: Select all
struct person
{
    int Age;
};

void
setAge(struct person* PersonStruct, int x)
{
    PersonStruct->Age = x; // notice pointers use -> instead of .
}

And when you call the function, you have to pass the address of the struct.
Code: Select all
struct person test_person;
setAge(&test_person, 18); // &variable gives the address of the variable (a pointer)
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Struct?

Postby SuperSonic » Sun Oct 28, 2012 1:08 am

Oh that's right. I forgot. However, wouldn't it be easier to use a reference parameter? For example:
Code: Select all
struct person
{
    int Age;
};

void setAge(struct person &PersonStruct, int x)
{
    PersonStruct.Age = x;
}
That way when you call the function, you can simply pass the name of the variable instead of the address:
Code: Select all
struct person test_person;
setAge(test_person, 18); // You no longer need the & sign
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Struct?

Postby skydereign » Sun Oct 28, 2012 1:13 am

SuperSonic wrote:I forgot. However, wouldn't it be easier to use a reference parameter?

It would, but you can't do that in C.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Struct?

Postby SuperSonic » Sun Oct 28, 2012 3:08 am

I'm so sorry. I've forgotten everything I used to know about C :o
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Previous

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron