Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 PointersIntroduction Dale Roberts, Lecturer Computer Science,

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 PointersIntroduction Dale Roberts, Lecturer Computer Science,"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 PointersIntroduction Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

2 Dale Roberts What is Pointer Bring your money to IUPUI SL-280 to exchange hostage “Find Instruction Under The crabapple tree next to old law school ” “Find next message on the top of traffic light on Michigan St. and West St. Phone rings and says “Deliver ransom to I-65 and West St. intersection Hostage Location A: Highway Intersection Location B: Traffic Light Location C: Crabapple Tree Location D: IUPUI SL-280 CS Dept. CSABCD Address of next location A pointer is an address.

3 Dale Roberts What is Pointer Pig Pen points to Lucy; Lucy points to Sally; Sally points to Linus; Linus points to Charlie; Charlie points to Snoopy; Snoopy has Woodstock Example: Find out who is with Woodstock among peanut gang ? Snoopy CharlieLinus … Pig Pen “ Lucy knows ” Lucy “Sally knows” Sally “ Linus knows ” Linus “ Charlie knows ” Charlie “ Snoopy knows ” Woodstock Woodstock is with Snoopy Snoopy An instance uses a pointer to link to a next instance making a chain.

4 Dale Roberts 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 (Snoopy’s pointer) and rewrite linus using variable of 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 _.

5 Dale Roberts Pointer Variable Declarations and Initialization PointersDefinition: A pointer is a variable that contains address of another variable. A powerful feature of C, but difficult to master Pointers enable programs to simulate call-by- reference and create/manipulate dynamic data structures Close relationship with arrays and strings

6 Dale Roberts Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value count 7 7 countPtr

7 Dale Roberts Pointer Declarations type *variable_name * used with pointer variables * used with pointer variables Example: int *myPtr; Declares a pointer to an int (pointer of type int * ) Multiple pointers require using a * before each variable declaration Can declare pointers to any data type Example: int *myPtr1, *myPtr2; float *pq; char *pc; Pointer Variable Declarations

8 Dale Roberts Pointer Variable Initialization Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing ( NULL preferred) Example: int *ptr, x; x=10; ptr = &x; 5000 ptr FFFF x 5000 ptr x 10 FFFF 5000 ptr FFFF x 10

9 Dale Roberts Pointer Operators & : Address Operator Returns address of operand int y = 5; int *yPtr; yPtr = &y; /* yPtr “points to” y */ /* yPtr gets address of y */ yptr 500000600000 y 5 yPtr y 5 address of y is the value of yPtr

10 Dale Roberts Pointer Operators * : Indirection / De-referencing Operator Returns a synonym/alias of what its operand points to *yptr returns y (because yptr points to y ) *yptr returns y (because yptr points to y ) * can be used for assignment that returns alias to an object * can be used for assignment that returns alias to an object *yptr = 7; // changes y to 7 Dereferenced pointer (operand of * ) must be a variable (no constants) * and & are inverses They cancel each other out

11 Dale Roberts Pointer Operators Example: int i = 5; int *pi; pi = &i; /* place the address of i into pi */ Assume Symbol Table i.e. &i = 874, i = 5; &pi = 902, pi = 874; *pi = ? *pi = 5; // same as i = 5; *pi = *pi * 2; // same as i = i * 2; *pi *= 2; // same as i *= 2; 874902 5 pii 874 variableaddressvalue at the address i 8745 pi 902874

12 Dale Roberts Example: int i,*pi,**ppi; i = 5; pi = &i; ppi = π 100104108 5 ppipii 100104 VariableAddressValue at address i1005 pi104100 ppi108104 UsageMeaningValue pi address of int 100 *piint value 5 &pi address of pointer 104 ppi address of pointer 104 *ppi address of int 100 **ppiint value 5


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 PointersIntroduction Dale Roberts, Lecturer Computer Science,"

Similar presentations


Ads by Google