Pointers and References

Hello everyone, I am having a VERY difficult time understanding the meaning of pointers and references. From what I understand so far, a pointer, "points" or obtains the value of the variable it points to. A reference to a variable basically "is" the variable and it references the address of the value of the variable in memory.
So for example:

int x = 0;
&x = 1;

Then 'x' would equal 1 right? Because I am basically changing the value of the 'x' variables value in memory?

Please let me know if I am wrong so far....


So I have read a few different textbooks on the topic of pointers and references, and I have taken a few classes (Currently in college) that briefly discuss pointers and references. I cannot find the answer to my question:

What is a real basic reason or example of "why" a person would want to reference something or "point" to a variable instead of just using the variable in an equation or something else as it stands? Why do we NEED references?


Thank you ahead of time, I just cannot seem to grasp the concept.


Graphitea

int x = 0;
&x = 1;

Then 'x' would equal 1 right? Because I am basically changing the value of the 'x' variables value in memory?

'x' wouldn't be equal to 1 because this &x = 1; basically tells your compiler to change the value of x's address to 1.
the right way to use the '&' operator is:
1
2
3
int x = 0;
int *px = &x;//declare a pointer of type int, assign the address of an int to *px
*px = 1;//use the pointer to change the value of x 



What is a real basic reason or example of "why" a person would want to reference something or "point" to a variable instead of just using the variable in an equation or something else as it stands? Why do we NEED references?

just search the site, please, there's too many of this type of question here already, and there are really good responses from the various gurus here on the subject
http://www.cplusplus.com/query/search.cgi?q=pointers
Thank you for your help. I will take a look at the link your provided and see if it starts to make more sense. I hope to find more real uses of the pointer so that I know when or how to use one when needed.


Graphitea
It is a little hard to wrap your head around the reason for references when you are first learning to code. The small, simple examples that you use to learn really don't benefit from references. But when you get further into programming, it should become clear. Let's say you have to pass a huge array of data to a function, or like in a game program, you have to pass an entire 3d environment to a function. If you pass the data by value, using the normal variable, then you end up having two copies of the variable in question, and that can start to eat up memory. But if you just pass by reference, then you are only passing the reference to the varable, and you save alot of overhead.
The small, simple examples that you use to learn really don't benefit from references.


This is probably the hardest thing I'm dealing with right now. In an ideal world, concepts would be introduced at the exact moment they are neccessary but it doesn't seem to be working out that way. Most example code I see in these two areas generally throw a pointer or a reference into a scenario where, in the context of the example, they really don't seem terrifically neccesary.

At least some of the documentation comes right out and says "you'll need to understand this stuff later" so for now I'm just trusting that I will.
KernalSeiden,

Thank you very much for your explanation. I was hoping for someone like you to come along and explain it briefly in common sense words I can understand. It makes much more sense to me now, so when I am viewing the tutorials I can think of the concepts in the way you mentioned.

Again, thanks


Graphitea
Topic archived. No new replies allowed.