The principles

java — Objects vs Primitive Data
   objects — have memory, methods, references to other objects

C/C++ — lvalue vs rvalue
   lvalue — persists beyond the expression
   rvalue — temporary:   5, '0', x+1
      x = y + 5 ;        is OK
      (x+0) = y + 5 ;    not OK
int *x ;
      x is lvalue
      *x is probably lvalue (or unassigned)
struct noteInfo n[10] ;
      n                is lvalue
      n[5]             is lvalue
      n[5].frequency   is lvalue

pointers can reference lvalues (storage)
in C, pointers have types (unlike PL/I)

& operator (address-of) can be applied to lvalue
* operator (indirection or deference) can be applied to pointer
   returns lvalue

TYPE l, m, n ;
TYPE *p, *q ;
TYPE **r ;

Illustration
    r = &p ;       OK
    r = &l ;       BAD
    p = &l ;       OK
    *p = VALUE;    OK
    **l = VALUE;   OK

Aliasing
    p = &l ;
    *p = VALUE1 ;
    q = &l ;
    *q = VALUE2 ;

Bad pointer
    x = *p ;   // where p not initialized
Almost always results in "segmentation violation"

Pointers, arrays and pointer arithmetic

pointers and arrays
    int N[] = {2, 3, 5, 7, 9} ;
    int *p = &N[2] ;
    p points to N[2]
    N is of type const int * (you can't assign 
    
Addition to pointers!!
    p+i is address of i'th integer after p
    p-i is address of i'th integer before p

    N[i] is *(N+i)
    p[i] is *(p+i)

    Access formally defined only when p+i or p-i point to elements of N

    If pointers reference elements of the same array 
        OR the inaccessible element one past the end of the array (weird)
    Pointers can be compared and subtracted

    p = &N[2] ;
    q = &N[5] ;
        p < q is true (1)
        p - q is -3
    p+q is not allowed
    *q  is not defined

    This also applies to multidimentional arrays....
      C is row major ...           X[2][3] is before X[2][4]
      Java multidimentional arrays are vectors of pointers
      FORTRAN is column major ... using dope vectors if appropriate
        Wikipedia on row vs column major order

C declarations with pointers

combining assignments and declarations may look a little odd int *p = &N[0] ; vs int *p ; p = &N[0] ; char const * p ; // *p = 'x' ; not allowed — p = q is OK char * const p ; // p = q ; not allowed — *p = 'x' is OK char const * const p ; // neither is allowed Also, check out qsort man page complicated declarations in C

Odds and ends

type punning

void *   — the generic pointer
  double x = 1/3.0 ;
  (long) rawbits = * (long *) (void *) &x ;

C++ reference type

void fetchAndAdd(int &x, int n) {
  x = x + n ;
}

Trying it out a bit