CISC-105 Lecture Notes 11/03/04 James Bonnett (I give Professor Conrad permission to copy/modify/post these notes) Edited/Reviewed by P. Conrad 12/10/04 Discussion on Pointers: (1) A pointer is an address. (2) An address is a pointer. (3) int x; means x is of type 'int' (4) int *x means x is of type (int *). (5) If p is of type int *, its contents are an address. The address is the location of 4 bytes of memory containing an int. (6) If p contains the address of an integer, we say that p points to that integer. (7) (For int *p) The expression (*p), when used in an expression, means "dereference P" or "follow the pointer." (*p) is the thing that p points to. (8) Note the difference in the meaning of the "*" when it appears in a declaration vs. when it appears in an expression. Here, these are declarations, and the meaning of the * is the p or q is a pointer variable (i.e. of type (int *), instead of type (int). int *p; \ void someFunction(int *p, int *q) |=> DECLARATIONS (DECLARE POINTER) void someOtherFuntion(int *p, int *q) / VS. in the expressions below the meaning of the * is that p is already a pointer variables, and we are "dereferencing" the pointer. That means that we are taking p, of type (int *) and following the pointer to the thing that p points to, so we have something that is of type int. (*p) = 7; \ x = (*p) + 3; \ (*p)++; \ EXPRESSIONS if (*p < 0) / (DEREFERENCE) { / printf("negative\n"); / } (9) If int *p is a pointer variable, we can make it point to an int variable, say int x, by using the unary & operator (the "address of" operator). int x = 7; int *p; p = &x; /* p is assigned the address of x */ Before (p = &x): After (p = &x): p [????] p [FFCA3094] x [ 7]<-- memory location FFCA3094 x [ 7] Here, FFCA3094 is the address of the memory cells containing x. That is what gets stored inside p. (10) If you use a "dereferenced pointer" on the left-hand side of an assignment statement, the variable pointed to (the "pointee") gets changed. int *p; int x = 7; p = &x; /* now p is a pointer pointing to x, x is the pointee of p */ (*p) = 8; printf("%d\n", x); /* prints 8, not 7 */ (11) The name of an array is a pointer to the element [0], that is, an array name is a pointer. A pointer is an address. An array name is an address, etc. Array Name "Trinity of pointers" // \\ Pointer = Address If I write (*p) vs. p[0], they mean EXACTLY the same thing. Passing in an array name is the same as passing in a pointer to the zeroth element. If I pass a regular variable to a function: The variable is passed by value and the variable is not changed in the calling function (usually 'main') If I pass the name of an array to a function: The variable is passed by reference, so the changes "stick" in the calling function. The remaining class time was a discussion of an extra credit opportunity. Extra credit is awarded to the first student to type up notes for lectures after midterm 1 and before midterm 2. For details, go to http://copland.udel.edu/~pconrad/cisc105/04F/extraCredit/extraCredit01.html