CS110_06b_pointers2 - EECS110: 6b Pointers: Declare,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 6b Pointers: Declare, Initialize, Apply Jack Tumblin jet@cs.northwestern.edu . (Recall) Setting Pointer Values How to use pointers: as (movable) array names Array index works for pointers. Move array names by ordinary integer math. Example: char *pW0,*pW1, *pW2, *pW3; pW0 pW1 pW2 pW3 = = = = msg; msg + 7; pW1 + 9; pW2 + 4; printf("%s %s %s %s",pW0,pW1,pW2,pW3); pW0 += 2; pW1 = pW1 +4; pW2 = pW3 +3; pW3 = pW3 -2; printf("%s %s %s %s",pW0,pW1,pW2,pW3); msg RESULT: > nasty, brutish, and short > sty, ish, rt d address . . n a s t y , \0 b r u t i s h , \0 a n d \0 s h o r t \0 pW0 pW0 pW1 pW1 pW2 pW3 pW3 pW2 (Recall) Using Pointers As `movable array names': array index works! As `movable array names':do address math using ints int i=0; char ch; while((pW0[i] != '\0') && (pW1[i] != '\0')) { // while both chars are not NULL ch = pW0[i]; // swap characters, and pW0[i] = pW1[i]; Result: pW1[i] = ch; i++; // go on to next char > brutis } > printf("%s %s\n",pW0,pW1); nasty,h, msg n a s t y , \0 b r u t i s h , \0 a n d \0 s h o r t \0 . . pW0 pW1 pW2 pW3 (Recall) Using Pointers As `movable array names': array index works! As `movable array names':do address math using ints char ch; while((pW0[0] != '\0') && (pW1[0] != '\0')) { // while both chars are not NULL ch = pW0[0]; // swap characters pW0[0] = pW1[0]; pW1[0] = ch; pW0++; // go on to next char pW1++; Result: } > brutis pW0 = msg; pW1 = pW0 +7; > printf("%s %s\n",pW0,pW1); nasty,h, msg n a s t y , \0 b r u t i s h , \0 a n d \0 s h o r t \0 . . pW0 pW1 pW2 pW3 (Recall)Use Pointers for Strings As `movable array names': array index works! As `movable array names':do address math using ints Pointers can define a string just as an array does: int i=0; printf("array msg printf("string at printf("string at holds pointer pointer string :%s pW0 is:%s pW1+3 is:%s \n",msg); \n",pW0); \n",pW1+3); Result: msg > > > array msg string at string at holds string :brutis pointer pW0 is:brutis pointer pW1 is:ty,h, . b r u t i s \0 n a s t y , h ,.\0 a n d \0 s h o r t \0 pW0 pW1 pW1+3 pW2 pW3 (Recall): #include <string.h> Two string functions we skipped before: all chars first n chars Find string length Copy a string Join 2 strings Compare 2 strings Find a char in a string Find a string in a string strlen() strcpy() strncpy() strcat() strncat() strcmp() strncmp() strchr() strrchr() strstr() String search: strchr(str1,ch) #include <stdio.h> #include <string.h> // for printf() // for strcpy() int main() { char msg1[81]={"Hello you!"}; char *fnd; // pointer-to-char fnd = strchr(msg1,'o'); printf("%s\n", fnd); } Result: > o you! > msg1 fnd search for char `ch', in string str1; return char pointer to 1st occurence H e l l o y o u ! /0 String search: strstr(str1,str2) #include <stdio.h> #include <string.h> // for printf() // for strcpy() int main() { char msg1[81]={"Hello you!"}; char *fnd; // pointer-to-char fnd = strchr(msg1,'o'); printf("%s\n", fnd); fnd = strstr(fnd,"yo"); printf("%s\n", fnd); } Result: > o you! > you! search for string str2 within str1; return char pointer to 1st occurence msg1 fnd fnd y o u ! /0 H e l l o Pointers So Far: Pointer: a new kind of variable that holds an address instead of a number or char Must declare and initialize before use: char *pW0; pW0 = msg; Pointers act like `movable array names' (like arrays), index them to access data: pW0[4] = `a'; (like arrays), use them as string variables:printf("%s",pW0); BUT you can move them using integer math: pW0 += 2; BUT they do not reserve (jargon: allocate) any array elements Pointers are VERY POWERFUL & DANGEROUS! ! C pointers let you read/write ANYWHERE in memory ! Point to Ordinary Variables... Pointers ==a new kind of variable that holds an address instead of a number or char Pointers can point ANYWHERE in memory, so How can we point to an ordinary (non-array) variable? ANSWER: the `address of' operator & (book: `address') Finds address of an ordinary variable (works for array elements, too!) `Address-of' Operator & Returns address where a variable is stored Use it to set the value of a pointer: int *pNum; // int ptr int count = 3; address-of or reference operator (it 'references' the variable count) Result: >num is 3 pNum = &count; printf("num is %d\n", pNum[0]); "Set the value of pointer pNum to the address of the integer variable count" count `Address-of' Operator & Returns address where variable is stored Use it to set the value of a pointer (Array elements are ordinary variables too!) int *pNum; // int ptr int trucks[2] = {2,3}; address-of or reference operator (it references variable trucks[1]) Result: >num is 3 pNum = &(trucks[1]); printf("num is %d\n", pNum[0]); "Set the value of pointer pNum to the address of integer array element trucks[1]" trucks[1] Pointers as Variables: Size? Pointer is a (special kind of) variable; holds an address The address is an integer: the pointer variable needs address in a 32-bit OS, 32 just 4 bytes* for any base type. 4 byte address in a 64-bit OS. 8 byte Real life analogy : a family of 8 does not need a bigger mailing address than a family of 4. Example: Result: double *pNum; // ptr to double double dist = 82.481; > dist is 82.481 > size: dist->8 bytes, pNum->4 bytes > pNum = &(dist); printf("dist is %f\n", pNum[0]); printf("size: dist->%d bytes,pNum-> %d bytes\n", sizeof(dist), sizeof(pNum)); Pointer `De-Referencing' by p[0] Referencing: point to an ordinary variable: &var Referencing pNum = &(dist); De-referencing: get or set the value where we point: referencing pNum[0]=3.2; Looks a bit silly "element 0 of a one-element array" There is another, nicer-looking method, but... Result: > dist is 82.481 > size: dist->8 bytes, pNum->4 bytes > double *pNum; double dist = 82.481; pNum = &(dist); printf("dist is %f\n", pNum[0]); printf("size: dist->%d bytes,pNum-> %d bytes\n", sizeof(dist), sizeof(pNum)); ...
View Full Document

Ask a homework question - tutors are online