Unformatted text preview:

1CMSC 212 –S07 (lect 8)Announcementsz Program #2– Due 2/27/07z Reading– Chapter 6 (today)– Chapter 11 (Thursday)2CMSC 212 –S07 (lect 8)Pointers Definedz Can declare any variable to be of type pointer– are similar to references in Java– use * before variable name to create a pointer to the type•int*foo;• myTypeDef *myPtr;• float *bar;– The type determines both:• what is pointed to• the size of the object being pointed toz Initialization– creating a pointer doesn't create the space it points to–int*foo;– *foo = 3; /* error: haven't defined what it points to */3CMSC 212 –S07 (lect 8)Pointersz Mixing types in declaration:– int *p, q;– declares p as a pointer to an integer and q as an integerz Can declare a pointer to a pointer– int **p; /* p pointer to a variable that points to an int */– useful if you want to modify a pointer in a subroutinez Typedefs of pointers improve readability– typedef int *intPtr;–intPtrp, q;z Function return type can be a pointer– Be careful of Dangling Pointers int *foo() {int x = 42;return (&x);}4CMSC 212 –S07 (lect 8)Pointer Arithmeticz Pointers are first class types– arithmetic applies to pointers just like ints.– This is useful, but can get trickyz Adding pointer and int– Adds n objects to value pointed to. • If objects are 100 bytes, p+1 is 100 bytes later in memory–Example:int x = 5;int a[10];int *p = a;*(p + 3) = 42; /* updates a[3] to 42 */z [] is really shorthand for pointer addition– a[n] is really the same as *(a + n)5CMSC 212 –S07 (lect 8)Pointer Assignmentsz Pointers are normal variables– they are not initialized unless assignedz Sample Assignments:– int *p, *q, i1, i2;– p = &i1; /* p now points to i1 */– q = p; /* q now points to i1 too */– p = &i2; /* p now points to i2 */– *p = *q; /* the value of i1 is copied to i2 */6CMSC 212 –S07 (lect 8)Relational Operations on Pointersz <, <=, >, >=, ==, !=• all compare two pointers of the same type• compares the memory location pointed toz Example:int *ptr;int *aPtr, *bPtr;if (!ptr) {…}If (aPtr < bPtr) {…}7CMSC 212 –S07 (lect 8)Type Conversionz C lets you assign variables of different types– called type conversion– useful when dealing with• external devices– int *fooDev = (int *) 100;– we know the foo device is at location 100• when type information read from a file is based on file contents.z WARNING: This can be very dangerousz (type *) variable:–int*x;– float pi = 3.14; – float *y = &pi;–x = (int*) y;8CMSC 212 –S07 (lect 8)Generic Pointersz void *– Declare a pointer that can point to any type– Any use are assignment requires a cast:• void *p;•int*x;•int*y;• p = (void *) x;• y = (int *) p;– Allows writing generic code• Need to ensure only appropriate things happen:–int*x;– float *y;– void *p;– p = (void *) x;– y = (float *) p;9CMSC 212 –S07 (lect 8)NULL Pointerz NULL is a pointer to nothing– (void *) 0 is its type– defined in stdlib.hz Can be used to initialize pointers–int*p;–p = NULL;z Can be used in comparison– if (p != NULL) {….–}z Often returned by functions to indicate error10CMSC 212 –S07 (lect 8)Pointer Examplessize_t strlen(char *str){size_t length;for (length=0; *str != '\0'; str++) {length++;}return length;}11CMSC 212 –S07 (lect 8)Array of Strings Exampleint findIt(char **table, char *it){int count = 0;while (*table) {if (!strcmp(*table, it)) {return count;}table++;count++;}return -1;}12CMSC 212 –S07 (lect 8)Const Keyword and Pointersz const int *p;– pointer to an integer that doesn't change:– const int x = 42;– p = &x; /* legal */– *p = 20; /* illegal - changing the constant */z int *const p;– constant pointer to an integer;– int *const p = &foo;– *p = 42; /* ok: just changing the value */– p = &bar; /* illegal: changing the pointer */13CMSC 212 –S07 (lect 8)Pointer Aliasesz Sometimes two pointers point to the same thing– This can be useful– This can cause problems:int *p, *q;int a= 89;p = &a;q = p;*p = 0;if (*q) { … }89qpa14CMSC 212 –S07 (lect 8)Other Pointer Operationsz Subtraction– pointer and int• p - n a pointer to the nth element before p– pointer and pointer• p - q number of items between p and q15CMSC 212 –S07 (lect 8)Pointers and Functionsz Parameters– can use const to prevent functions from changing valuesint frob(const char *data, int size) {if (data[0] == 'Q') { .. } /* OK: just using the data */data[2] = 'A'; /* Error: can't modify data */}z Functions can return pointer typesint *getStuff(int *a, *b) {if (*a > *b ) {return a;} else {return


View Full Document

UMD CMSC 212 - Lecture Slides

Download Lecture Slides
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Slides and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Slides 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?