Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers Introduction CSCI 230

Similar presentations


Presentation on theme: "Pointers Introduction CSCI 230"— Presentation transcript:

1 Pointers Introduction CSCI 230
Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Introduction Dale Roberts, Lecturer Computer Science, IUPUI

2 What is Pointer A pointer is an address. Hostage 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 CS A B C D Address of next location A pointer is an address.

3 What is Pointer Example: Find out who is with Woodstock among peanut gang? Pig Pen “ Lucy knows ” Lucy “Sally knows” Sally “ Linus knows ” Linus “ Charlie knows ” Charlie “ Snoopy knows ” Woodstock Woodstock is with Snoopy 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 Snoopy Charlie Linus 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 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 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 int *myPtr1, *myPtr2; float *pq; char *pc;

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

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

10 * : Indirection / De-referencing Operator
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 Pointer Operators i pi Example: 874 902 5 pi i 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; variable address value at the address i 874 5 pi 902 874 902 5 pi i

12 Example: int i,*pi,**ppi; i = 5; pi = &i; ppi = π i 100 5 pi 104
Variable Address Value at address i 100 5 pi 104 ppi 108 100 104 108 5 ppi pi i Usage Meaning Value pi address of int 100 *pi int value 5 &pi address of pointer 104 ppi *ppi **ppi


Download ppt "Pointers Introduction CSCI 230"

Similar presentations


Ads by Google