t13APointersIntroduction - Pointers Introduction Dale...

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

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Pointers Introduction Dale Roberts 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 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 … 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 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 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 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 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 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 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 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 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 int value ...
View Full Document

  • Spring '10
  • kittu
  • Pointer, Dale Roberts, Dale Roberts www.kite-legends.blogspot.com

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern