Department of Computer and Information Science,
School of Science, IUPUI
CSCI 230
Pointers
Introduction
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: [email protected]
Dale Roberts
What is Pointer
CS Dept.
Location A:
Highway Location C:
Bring your
Intersection Crabapple
money to
Tree
IUPUI SL-280
to exchange
Phone rings Find next hostage
and says message
Deliver on the top of
ransom to traffic light
I-65 and on Michigan
West St. St. and
intersection West St. Location D:
IUPUI SL-280
Find
Location B:
Instruction
Traffic Light
Under
The crabapple
tree next to old
law school
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 among peanut gang?
Charlie
Pig Pen Sally Linus
Lucy Woodstock
Snoopy
Lucy knows Linus knows Charlie knows Snoopy knows Woodstock is with Snoopy
Sally knows
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 Snoopy
instance making a Linus Charlie
chain. Dale Roberts
Pointer declarations:
datatype snoopy, *charlie, **linus;
snoopy = ; /* snoopys content is Woodstock */
charlie = &snoopy;
/* charlies content is the info (pointer) to locate snoopy, which is snoopys address*/
linus = &charlie;
/* linuss content is the info (pointer) to locate charlie, which is charlies address*/
In general, we can rewrite charlie using variable of snoopyPtr
(Snoopys pointer) and rewrite linus using variable of
snoopyPtrPtr (pointer to Snoopys 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 _.
Dale Roberts
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
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
value (indirect reference)
countPtr count
7
Indirection referencing a pointer value
Dale Roberts
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;
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;
ptr x
FFFF
5000
ptr x
x=10; FFFF
5000 10
ptr x
FFFF 10
ptr = &x; 5000 FFFF
Dale Roberts
Pointer Operators
&: Address Operator
Returns address of operand
yPtr y
int y = 5;
5
int *yPtr;
yPtr = &y;
/* yPtr points to y */
/* yPtr gets address of y */
yptr y
500000 600000 600000 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
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
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
pi i
i 874 5 902 874 874
5
pi 902 874
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;
Dale Roberts
Example:
Variable Address Value at address
int i,*pi,**ppi;
i 100 5
i = 5;
pi 104 100
pi = &i;
ppi 108 104
ppi = π
ppi pi i
108 104 100
104 100 5
Usage Meaning Value
pi address of int 100
*pi int value 5
&pi address of pointer 104
ppi address of pointer 104
*ppi address of int 100
**ppi int value 5
Dale Roberts