1 / 12

Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

Department of Computer and Information Science, School of Science, IUPUI. CSCI 230. Pointers Introduction . Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. CS. A. B. C. D. Address of next location. What is Pointer. CS Dept.

leone
Download Presentation

Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. CS A B C D Address of next location 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 D: IUPUI SL-280 “Find Instruction Under The crabapple tree next to old law school ” Location B: Traffic Light Hostage A pointer is an address.

  3. Pig Pen Lucy Sally Linus Charlie Woodstock “Sally knows” “ Linus knows ” “ Lucy knows ” “ Charlie knows ” “ Snoopy knows ” Snoopy Woodstock is with Snoopy … Snoopy Linus Charlie What is Pointer Example: Find out who is with Woodstock among peanut gang? 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 instance making a chain.

  4. 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. Pointer Variable Declarations and Initialization • Pointers • Definition: 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. count 7 • 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 countPtr count 7

  7. Pointer Variable Declarations • Pointer Declarations type *variable_name • * 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;

  8. ptr x FFFF 5000 x ptr FFFF 5000 10 ptr x FFFF 5000 FFFF 10 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;

  9. yPtr y 5 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 y 5 500000 600000 600000 address of y is the value of yPtr

  10. Pointer Operators • * : Indirection / De-referencing Operator • Returns a synonym/alias of what its operand points to • *yptr returns y (because yptr points to y) • * 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. pi i 902 874 874 5 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;

  12. ppi pi i 108 104 100 5 104 100 Example: int i,*pi,**ppi; i = 5; pi = &i; ppi = π

More Related