Sign in to follow this  
deathwearer

Question about a tutorials

Recommended Posts

I'm curently reading a tutorials on pointers. But there is some stuff that is a bit different that what I'v learn in my programmation classes. ( took from that tutorials http://www.gamedev.net/reference/articles/article1930.asp ) (the tutorials exemple) int main(void) { int hello = 0; SomeFunction(&hello); return 0; } void SomeFunction(int* value) { (*value) ++; } He did this that way, alright i understand what it does, but isn't simple to do this like that? or it is not the same thing? (my exemple) int main(void) { int hello = 0 SomeFunction(hello); return 0; } void SomeFunction(int &value) { value++; }

Share this post


Link to post
Share on other sites
Both methods will have the same effect, but a reference, technically, isn't a pointer, its an alias.

"The major differences between references and pointers are that there are no null references, all references require initialization, and a reference always refers to the object with which it is initialized." -Common C++ Knowledge (13)

Share this post


Link to post
Share on other sites
Quote:
Original post by deathwearer
( took from that tutorials http://www.gamedev.net/reference/articles/article1930.asp )

*** passing an address ***

He did this that way, alright i understand what it does, but isn't simple to do this like that? or it is not the same thing?

&&& passing a reference &&&

You should try and pass a const reference (or a reference when you want to modify the object) wherever possible. Passing pointers is useful, as Mushu stated, because pointers allow you to deal with objects that may not always exist and be available to you, or when you want the choice between some provided functionality, like (optionally) passing an address of a struct you want filled.

References and pointers both eliminate the need for the - possibly expensive - copies that passing by value induces, but pointers comparatively have the potential to point to different objects, such as when you're iterating through an array. The syntax of a pointer can also be somewhat confusing at times, with its intention not immediately obvious, at least much less than that of a reference (which has identical usage syntax as the object itself, since it is the object).

Share this post


Link to post
Share on other sites
yeah the & is used in two ways. now its taken me a while to understand this so ill post and let people corrent me, im still learning

if you have:

myvar = 100;
somefunction(&myvar)

what you do here is pass a referece of myvar, this meaning whatever happens to my var in the function will change the contents stored in myvar

see the problem is scope, if you didnt pass a reference of myvar then what you would be doing is passing the value stored at myvar, the fucntion does what it needs to to that number but when the fucntion exits my var is still 100 no matter what happened in the function

so for eg

main()
myvar = 100;
somefunction(&myvar) //value of my var here is 100

//do func stuff

//value at my var here is not 100 or it may be the case that is stays at 100

if you do

main()
myvar = 100;
somefunction(myvar)//value of my var here is 100

//do func stuff

//value at my var here is still 100


the other thing the & stands for is the address of and the compiler knows when you are passing by reference or you want the address of a variable.

pointers are variables that store memory addresses so:

int *p // p is a pointer to an int value
int Value = 10 // the int 'Value' is assigned to 10.

Value is stored in comp memory and it has its own address so values address in memory could be 10000, for example.

When you are elite at programming you may need to know this address so you use a pointer to stored the memory address of the variable and you do it like this

int *p; //pointer to an int
int Value = 10 ;//an int
p = &Value; p is a pointer to the address of an int, this for example would be wrong

int *p; //pointer to an int
char Value; ;//an int
p = &Value; //value isnt an int and p can only point to an int

so, if you printed out p like this

cout << p << endl; you would get the 10000 memaddress (in reality it may look like a hex value)

now this address holds the variable Value which is assigned to 10, if you wanted the value stored at that memory address you have to do it this way

cout << *p << endl which is called dereferencing the pointer so the output would on screen here would be 10.

my explanation may be incorrect a pm would be better, pointers are goin to be the nightmare of ur life but they are necessary and there is no avoiding them

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this