pointers, references and variables in any language

Please explain the diffenence between pointers, references and variables

This is a python example but the pointers, references and variables is in c++, so I can not test
http://www.experts-exchange.com/Programming/Languages/Scripting/Python/A_6589-Python-basics-illustrated-part-2.html

Is there a way I can test.
For example w3schools.com makes it easy to type in code into a text editor
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
In all languages a variable stores a value.  The value can be a number, a string, a floating point value etc.

The value inside a variable can also be a memory location.  When it is, then we call that either a pointer or a reference.

A pointer always means a variable that stores an explicit memory location.  Usually the language allows that pointer to be changed, to point to any other memory location you choose - including completely invalid ones.  (Languages like C++ allow this).

The meaning of a 'reference' can vary a little between languages.  But generally, if the memory location is required by the language to be the address of another object, then we call that a reference.  (Languages like Java only allow references - you can't create a pointer to anything except another object).

So we have:
 a) Variable - something which can store a value
 b) Pointer - a value that is a specific memory location (the location is 'pointed' to by the variable)
 c) Reference - a value that is the memory location of another object in the language (i.e. more specific than a pointer which can point to arbitrary places in memory - not just other objects).

Hope that helps,

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peprCommented:
+1 to dpearson explanation. To add for reference. To understand it, you should think in terms reference value and reference variable. Similarly, you can think in terms pointer value and pointer variable. The reference value and the pointer value are exactly the same and it is the memory address of the target. The pointer variable is related to the space where the pointer value is stored, the reference variable is related to the space where the reference value is stored. As the values are memory addresses in both types, both variables (pointer and reference) need the same memory space to store its value -- 4 bytes in a 32-bit operating system, or 8 bytes in a 64-bit operating system).

The main difference between pointers and references (in C++) is that for indirect access to the target through the pointer variable, the pointer must be dereferenced explicitly using the *ptr when accessing the pointed target as a whole, or using the ptr->part when accessing the part of the referenced target.

For indirect access through the reference variable (say named ref that stores the address), using ref means automatic dereferencing. Logically and syntactically it is exactly the same as with working with the target directly. So, when accessing the part of the referenced target, you write ref.part.

Technically, both pointers and references has the same implementation -- indirect access through the stored address. Working with pointers is more explicit. There also is a special constant nullptr (or NULL in earlier standards and also in the C language) that can be assigned to the ptr variable with meaning pointing to nowhere. The reference variable is always required to point to a valid target (i.e. more restrictive) so that it could always be used syntactically as if working with the target directly.

In C++ and the other compiled languages, variable names are converted to addresses. There is no need to keep the string name in the binary program. A lot of variables is accessed directly. Only when you explicitly say, the target can be allocated dynamically (the exact address known only in runtime) and reference via pointers or references. In Java, every object is created dynamically and access via references (i.e more restrictive rules in comparison to pointers and therefore simpler to be used by the programmer). In Python, every object is created dynamically the similar way. The reference variables are always placed in one of the internal Python dictionaries where the string name of the variable is the key, and reference value (the address of the target) is the value of the dictionary item.
0
rgb192Author Commented:
so reference is storage space of a variable value

and pointer is storage space of a reference value?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

peprCommented:
Reference in the abstract sense is the value that refers to the target object (where the object can be as simple as an integer variable). The concrete implementation of a reference is the address of the target stored in another variable (speaking about compiled languages as C++, for example). If that dereferencing is done automatically, then it looks as if the extra variable did not exist and as if one worked directly with the target object. But it is only the illusion -- the access is indirect. If the dereferencing is done explicitly, then we call it a pointer.

When speaking about a pointer, we can think about both the pointer variable and the address of the target stored inside. We can think this way also when thinking about a reference. However, a reference usually mimics the target object so well that some people tend to think about it (incorrectly) as about the target object itself.
0
sarabandeCommented:
I would like to add some samples o the great explanations given above:

(A) by value

void f(string s) 
{ 
      s += " World";
      cout << s; 
}

string str = "Hello";
f(str);
cout << str;  // prints "Hello"

Open in new window


the string str was passed as a copy to the function f. the compiler allocates a new memory space on the stack with size of string and then the copy constructor of string was called to create a copy. whatever f does with the variable s, the caller's str would not change its contents. when the control returned from f to the caller the copy s was deleted and the storage was freed.

(B) by pointer

void f(string * s) 
{ 
      if (s != NULL)
      {
             *s += " World";
             cout << s->c_str() << endl;
             s = new string("some other text");
             cout << s->c_str() << endl;
      }  
}

string str = "Hello";
f(&str);
cout << str;  // prints "Hello World"

Open in new window


the string str was passed by pointer (address) to the function f. the compiler allocates a new memory space on the stack with size of pointer and then the address of string str was copied to the new pointer variable.  if pointer s was dereferenced in f and the string where s is pointing to was changed then the caller's str also would change as the pointer still points to the original storage. but if a new address was assigned to the pointer variable, it doesn't have any impact to the original string.

(B) by reference

void f(string & s) 
{ 
     s += " World";
     cout << s.c_str() << endl;
     s = "Another World";
}

string str = "Hello";
f(str);
cout << str;  // prints "Another World"

Open in new window


the string str was passed by reference (address) to the function f. the compiler allocates a new memory space on the stack with size of pointer and then the address of string str was copied to the storage.  the string s in f is an alias of the caller's str. if it was changed in f the caller's str also would change. the stack storage is same as when passing by pointer but no pointer variable was created. the stack storage only serves to pass the address of the original variable,

Sara
0
rgb192Author Commented:
thanks for all advice.
I am starting to understand.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.