using * for creating variables

Game Editor comments and discussion.

using * for creating variables

Postby asmodeus » Thu Mar 06, 2008 12:32 pm

What's the different from int and int * in the script editor?
Code: Select all
int var;
int *var2;
char mychar1;
char *mychar2;
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score

Re: using * for creating variables

Postby edh » Thu Mar 06, 2008 1:06 pm

Int is basically asking for a place to hold an integer (non-real) number.

Int* is asking for a place to hold a pointer to an integer. A pointer is the address of something else in memory. If you have a pointer to an actor, then you have a variable with the address of an Actor which is somewhere else in memory. Hopefully. :wink:

example:
Code: Select all
int integer;
int *pointer;

integer = 5; // put 5 in 'integer'
pointer = &integer; // put the address of 'integer' into 'pointer' (this is the location of 'integer' in memory).

// if you print integer here, it will be 5 and so will pointer if you dereference (jump to the memory address stored in) it.

integer = 6; // put 6 in 'integer'

// if you print integer, it will be 6 and so will pointer



Same for char* and actor*. They point to some other location in memory which is the type you specify.
User avatar
edh
 
Posts: 233
Joined: Thu Sep 13, 2007 12:17 am
Location: Maine, USA
Score: 14 Give a positive score

Re: using * for creating variables

Postby asmodeus » Thu Mar 06, 2008 1:13 pm

Ok. For example I write:
Code: Select all
integer = 5;
pointer = &integer;

and later I change the value of integer, pointer is automatically changed?
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score

Re: using * for creating variables

Postby edh » Thu Mar 06, 2008 1:23 pm

Effectively, yes.
Technically, no.

Maybe this will explain: pointer is always pointing to integer (unless you change it with pointer = &integer2 or something). But, when you print out the value of *pointer, you get whatever integer has currently.

So, it's sort of automatically changed, but nothing actually happens to pointer technically.
User avatar
edh
 
Posts: 233
Joined: Thu Sep 13, 2007 12:17 am
Location: Maine, USA
Score: 14 Give a positive score

Re: using * for creating variables

Postby edh » Thu Mar 06, 2008 1:28 pm

Here is a good example (if you ignore the fact that it says pointer = [Sigma] or the math simple for sum instead of the variable sum;

http://www.exforsys.com/tutorials/c-lan ... nters.html

Here is the bad example rewritten:
int *ptr;
int sum;
sum=45;
ptr=sum;
printf (“\n Sum is %d\n”, sum);
printf (“\n The sum pointer is %d”, ptr);

Note: that example shows that ptr holds the address of sum, not the value of sum.

You have to dereference it to see sums value. Like this:

printf (“\n The sum pointer is %d”, *ptr);
User avatar
edh
 
Posts: 233
Joined: Thu Sep 13, 2007 12:17 am
Location: Maine, USA
Score: 14 Give a positive score

Re: using * for creating variables

Postby Bee-Ant » Thu Mar 06, 2008 4:37 pm

when using pointer, you can show the memory address or something... :D
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron