**************** POINTERS Draw the following -- as boxes, not as in memory int p, *q, **r, V[10], M[10][10] ; C has typed pointers (like Pascal) PL/1 POINTER --> Pascal ^integer --> C *int --> C++ *int --> Java Integer? In Pascal.... @P rather than &P P^ to dereference a pointer ^t to declare C has NULL (like nil in LISP) LISP nil --> Pascal nil --> C NULL --> C++ NULL --> Java null lvalue has a location, but 'l' stands for 'left' int *p[5] ; // p is an array of 5 integer pointers int (*p)[5] ; // p is an pointer to an array of 5 integers **************************************************************** Pointer arithmetic A much despised feature of C and C++ (and Pascal) int V[10], *p, *q ; p = &V[0] ; *p = 5 ; // Sets V[0] What is p+0? Should be &V[0] .... right! What is p+3? Should be &V[3] .... right! What is p+i? Should be &V[i] .... right! What is *(p+i) Should be V[i]! What is *(p+i) Should be p[i] !! V is really a const int * !! V[i] is *(V+i) ++p Move to next integer --p Move to previous integer p = p + 3 Move to third integer IN ARRAY p = &V[i] ; q = &V[j] ; Than q-p should be same as j-i !!! Obviously **************** STRUCTURES COBOL record --> PL/1 record PASCAL record --> /C struct --> C++ class --> Java class struct P2type { double x ; double y ; } typedef struct P2type P2 ; // but *NOT* in C++ P2 a, b ; Box with subboxes ---- a.x = 5 ; double length2D(P2 a) { // copies return sqrt(a.x*a.x + a.y*a.y) ; } struct P2 *a ; Reference is (*a).x, but abbreviates to a->x double length2D(P2 *a) { return sqrt(a->x*a->x + a->y*a->y) ; } void lengthen2D(P2 *a) { a->x = 2*a->x ; a->y = 2*a->y ; } P2 * lengthen2D(P2 *a) { P2 *R = (P2 *)malloc(sizeof(P2)) ; if (R != null) { R->x = 2*a->x ; R->y = 2*a->y ; } return R ; } Java P2 V = new P2(1,2,3) ; ... garbage collect when no longer needed, may need finalize C++ P2 V = new P2(1,2,3) ; .... delete P2 ; or { V P2(1,2,3) ; .... call destructor (~) if needed } C P2 V = malloc(....) ; free(V) ;