Page 1 of 1

using * for creating variables

PostPosted: Thu Mar 06, 2008 12:32 pm
by asmodeus
What's the different from int and int * in the script editor?
Code: Select all
int var;
int *var2;
char mychar1;
char *mychar2;

Re: using * for creating variables

PostPosted: Thu Mar 06, 2008 1:06 pm
by edh
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.

Re: using * for creating variables

PostPosted: Thu Mar 06, 2008 1:13 pm
by asmodeus
Ok. For example I write:
Code: Select all
integer = 5;
pointer = &integer;

and later I change the value of integer, pointer is automatically changed?

Re: using * for creating variables

PostPosted: Thu Mar 06, 2008 1:23 pm
by edh
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.

Re: using * for creating variables

PostPosted: Thu Mar 06, 2008 1:28 pm
by edh
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);

Re: using * for creating variables

PostPosted: Thu Mar 06, 2008 4:37 pm
by Bee-Ant
when using pointer, you can show the memory address or something... :D