t13APointersIntroduction - Pointers Introduction Dale...

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: Pointers Introduction Dale Roberts www.kite-legends.blogspot.com What is Pointer CS Dept. Location A: Highway Intersection Location C: Crabapple Tree Bring your money to IUPUI SL-280 to exchange hostage Phone rings and says “Deliver ransom to I-65 and West St. intersection “Find next message on the top of traffic light on Michigan St. and West St. Location B: Traffic Light “Find Instruction Under The crabapple tree next to old law school ” Location D: IUPUI SL-280 Hostage CS A B C D A pointer is an address. Address of next location Dale Roberts www.kite-legends.blogspot.com What is Pointer Example: Find out who is with Woodstock Pig Pen Lucy Sally Linus among peanut gang? Charlie Woodstock “ Lucy knows ” “Sally knows” “ Linus knows ” “ Charlie knows ” noopy knows ” “S Snoopy Woodstock is with Snoopy Pig Pen points to Lucy; Lucy points to Sally; Sally points to Linus; Linus points to Charlie; Charlie points to Snoopy; Snoopy has Woodstock An instance uses a pointer to link to a next Linus Charlie instance making a chain. Dale Roberts www.kite-legends.blogspot.com … Snoopy Pointer declarations: datatype snoopy, *charlie, **linus; snoopy = ; /* snoopy’s content is Woodstock */ charlie = &snoopy; /* charlie’s content is the info (pointer) to locate snoopy, which is snoopy’s address*/ linus = &charlie; /* linus’s content is the info (pointer) to locate charlie, which is charlie’s address*/ In general, we can rewrite charlie using variable of snoopyPtr using (Snoopy’s pointer) and rewrite linus using variable of (Snoopy’s and using snoopyPtrPtr (pointer to Snoopy’s pointer); Note that this is only a naming convention. The only requirement is that the variable be a valid identifier: begin with a letter followed by letters, digits or _. letters, Dale Roberts www.kite-legends.blogspot.com Pointer Variable Declarations and Initialization Pointers Pointers Definition: Definition: A pointer is a variable that contains address pointer of another variable. of A powerful feature of C, but difficult to master Pointers enable programs to simulate call-byPointers simulate reference and create/manipulate dynamic data reference structures Close relationship with arrays and strings Close Dale Roberts www.kite-legends.blogspot.com Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) count 7 Pointers contain address of a variable that has a specific Pointers value (indirect reference) value countPtr count 7 Indirection – referencing a pointer value Indirection Dale Roberts www.kite-legends.blogspot.com Pointer Variable Declarations Pointer Declarations type *variable_name * used with pointer variables Example: int *myPtr; int Declares a pointer to an int (pointer of type int *) Declares int int Multiple pointers require using a * before each variable Multiple declaration declaration Can declare pointers to any data type Example: int *myPtr1, *myPtr2; float *pq; char *pc; Dale Roberts www.kite-legends.blogspot.com Pointer Variable Initialization Initialize pointers to 0, NULL, or an address Initialize NULL 0 or NULL – points to nothing (NULL preferred) NULL Example: int *ptr, x; 5000 ptr FFFF x x=10; 5000 ptr FFFF x 10 x 10 ptr ptr = &x; 5000 FFFF FFFF Dale Roberts www.kite-legends.blogspot.com Pointer Operators &: Address Operator Returns address of operand int y = 5; int *yPtr; int yPtr = &y; yPtr /* yPtr “points to” y */ /* “points /* yPtr gets address of y */ /* gets yptr y 5 500000 600000 600000 yPtr y 5 address of y is the value of yPtr Dale Roberts www.kite-legends.blogspot.com Pointer Operators * : Indirection / De-referencing Operator Returns a synonym/alias of what its operand Returns points to points *yptr returns y (because yptr points to y) *yptr yptr * can be used for assignment that returns alias to an object an *yptr = 7; // changes y to 7 Dereferenced pointer (operand of *) must be a Dereferenced must variable (no constants) variable * and & are inverses They cancel each other out They Dale Roberts www.kite-legends.blogspot.com Pointer Operators Example: int i = 5; int *pi; pi = &i; /* place the address of i into pi */ Assume Symbol Table variable address value at the address i pi 874 902 5 874 pi 902 874 874 i 5 i.e. &i = 874, i = 5; &pi = 902, pi = 874; *pi = ? *pi = 5; *pi *pi = *pi * 2; *pi *pi *= 2; *pi // same as i = 5; // // same as i = i * 2; // // same as i *= 2; // Dale Roberts www.kite-legends.blogspot.com Example: int i,*pi,**ppi; i = 5; pi = &i; ppi = π 108 Variable Address Value at address i pi ppi 104 100 104 108 i 5 Value 5 100 104 ppi 104 pi 100 100 Usage pi *pi &pi ppi *ppi **ppi Meaning address of int int value address of pointer address of pointer address of int 100 5 104 104 100 5 Dale Roberts www.kite-legends.blogspot.com int value ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online