=============================== DEFINITIONS ============================== Value: In computer science, a value is a sequence of bits that is interpreted according to some data type. Reference: In computer science, a reference is an object containing information which refers to data stored elsewhere, as opposed to containing the data itself. Accessing the value referred to by a reference is called dereferencing it. References are fundamental to constructing many data structures (such as linked lists) and in exchanging information between different parts of a program. Pointer In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. Obtaining the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++). ==================================================================== Language designers definitions (sometimes): define function(B) { foobarbaz.... } execute: A[j] = foo function(A[j]) * Call-by-value. Pass the r-value of A[j]. * Call-by-reference. Pass the l-value of A[j]. * Call-by-name. Pass the text of A[j] itself, avoiding "name clashes." ================================= C ================================= pass by value semantics --------------------------------------------------------------------------- int a = 5; int *money = NULL; money = &a; *money = 8; malloc(int variable) free(pointer) --------------------------------------------------------------------------- --------------------------------------------------------------------------- void alter(int *n) { *n = 120; } void func(void) { int x = 24; alter(&x); } --------------------------------------------------------------------------- HUGE BUGS: --------------------------------------------------------------------------- int *ptr = malloc(sizeof (int)); free(ptr); *ptr = 0; /* Undefined behavior! */ --------------------------------------------------------------------------- ------------------------------------------------- int array[5]; /* Declares 5 contiguous integers */ int *ptr = array; /* Arrays can be used as pointers */ ptr[0] = 1; /* Pointers can be indexed with array syntax */ *(array + 1) = 2; /* Arrays can be dereferenced with pointer syntax */ --------------------------------------------------------------------------- --------------------------------------------------------------------------- ================================= C++ ================================= --------------------------------------------------------------------------- --------------------------------------------------------------------------- void swap ( int lhs, int rhs) { temp = lhs; lhs = rhs; rhs = temp; } void swap ( int& lhs, int& rhs);} int temp = lhs; lhs = rhs; rhs = temp; } --------------------------------------------------------------------------- --------------------------------------------------------------------------- int& preinc(int& x) { ++x; return x; } preinc(y) = 5; // same as ++y, y = 5 --------------------------------------------------------------------------- What does "const Fred* p" mean? = Fred const* x It means p points to an object of class Fred, but p can't be used to change that Fred object (naturally p could also be NULL). What's the difference between "const Fred* p", "Fred* const p" and "const Fred* const p"? You have to read pointer declarations right-to-left. * const Fred* p means "p points to a Fred that is const" * Fred* const p means "p is a const pointer to a Fred" * const Fred* const p means "p is a const pointer to a const Fred" const Fred& x = Fred const& x It means x aliases a Fred object, but x can't be used to change that Fred object. Fred& const x = stupid --------------------------------------------------------------------------- ================================= Java =================================== --------------------------------------------------------------------------- public void swap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; } --------------------------------------------------------------------------- public void swap(Integer var1, Integer var2) { Integer temp = var1; var1 = var2; var2 = temp; } --------------------------------------------------------------------------- public void tricky(Point arg1, Point arg2) { arg1.x = 100; arg1.y = 100; Point temp = arg1; arg1 = arg2; arg2 = temp; } public static void main(String [] args) { Point pnt1 = new Point(0,0); Point pnt2 = new Point(0,0); System.out.println("X: " + pnt1.x + " Y: " +pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); System.out.println(" "); tricky(pnt1,pnt2); System.out.println("X: " + pnt1.x + " Y:" + pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); } X: 0 Y: 0 X: 0 Y: 0 X: 100 Y: 100 X: 0 Y: 0 --------------------------------------------------------------------------- ============================= PYTHON ==================================== --------------------------------------------------------------------------- def foobar(a): a = 0 # rebinds a locally, produces no side-effect a = 1 foobar(a) # nothing happens a = [1, 2, 3] foobar(a) # a points to [1,2,3] >def foobar(a): > a *= 2 # rebinds a only if a points to an immutable type, > # otherwise the value pointed to by a is changed. Remember, # this is because compound operators such as *= have side effects a = 1 foobar(a) # nothing happens a = [1, 2, 3] foobar(a) # a points to [1,2,3,1,2,3]