CSC404 Chapter1
CSC404 Chapter1
Pointers
Learning Outcomes
At the end of these lessons, students should be
able to:
Describe the pointer data type and pointer
variables.
Declare and manipulate pointer variables.
Use pointer operators: address operator (&)
and dereferencing/indirection operator (*).
Use the new and delete operators to
manipulate dynamic variables.
Describe operations on pointer variables
What is Pointer?
Pointer variables, simply called pointers, is a
variable whose content is an address (that is, a
memory address)
Normally, a variable contains a specific value, e.g., an
integer, a floating-point value and a character.
However, a pointer contains the memory address of a
variable that in turn contains a specific value.
pCount count
data_type *pointer_variable_name;
float *value;
value = NULL;
Create a pointer named value and initialize it to NULL; the pointer can
store the address of a float variable.
The * and & Operators
The * operator:
In front of a pointer variable
An unary operator that returns the value of the variable
located at the address that follows – get data that pointer
variable points to
Also known as the dereferencing operator or indirection
operator
The & operator:
In front of an ordinary variable
An unary operator that returns the memory address of its
operand/variable – determines “address of ” variable
Also known as the address-of-operator
Cont…
For example, consider the declarations,
double *p, v;
p = &v;
p v p v
? ? 2000 ?
2000
*p = 9.99;
p v
9.99
Referring to a Variable
Recall:
double *p, v;
p = &v;
p1 = p2;
Before After
p1 8 p1 8
p2 9 p2 9
Cont…
Do not confuse with:
*p1 = *p2;
Assigns “value pointed to” by p1, to “value pointed to” by p2
*p1 = *p2;
Before After
p1 8 p1 9
p2 9 p2 9
Example
The value of count is 5
int main() The address of count is 0x0012ff50
{ The address of count is 0x0012ff50
int count = 5; The value of count is 5
int *pCount = NULL;
The address of pCount is 0x0012ff4c
pCount = &count;
float *f;
char *d, e;
Exercise 2
What is the output produced by the following code?
int *p1, v1;
v1 = 0;
p1 = &v1;
*p1 = 42;
cout << v1 <<endl;
cout << *p1 <<endl;
p q r
a b c
After the data have been read, the program reassigns the pointers
so that p points to c, q points to a and r points to b. after making
the reassignments, the program prints the variables using the
pointers. For each variable, print both its contents and its address.
Exercise 5
55 + 11 is 66
Dynamic Variable
*p2 = 53;
cout<<“*p1 == ”<<*p1<<endl;
cout<<“*p2 == ”<<*p2<<endl;
p1 = new int;
*p1 = 88;
cout<<“*p1 == ”<<*p1<<endl;
cout<<“*p2 == ”<<*p2<<endl;
p1 ? p1
42
p2 ? p2
p = new int;
*p = 5;
x = *p + 1;
y = *p;
q = new int;
*q = y + 3;
cout<<x<<“ ”<<y<<“ ”<<*p<<“ ”<<*q<<endl;
Exercise 2
What is the output for the following program segment:
char ch = ‘p’;
int a=2, b=5;
int *ptra = &a, *ptrb = &b, *ptrc;
char *ptrch1 = &ch, *ptrch2;
cout<<*ptra<<“ ”<<*ptrb<<“ ”<<*ptrch1<<endl;
*ptra = a + 20;
ptrc = new int(99);
ptrb = ptrc;
*ptrc = *ptrb + 10;
ptrch2 = new char(‘h’);
ch = ‘y’;
cout<<*ptra<<“ ”<<*ptrb<<“ ”<<*ptrc<<endl;
cout<<*ptrch1<<“ ”<<*ptrch2<<endl;
Exercise 4
Give the output from this code fragment:
int *p;
p = new int;
*p = 1234;
cout<<*p<<endl;
delete p;
cout<<*p<<endl;
After p has been deleted, p still points to memory cell that has
been de-allocated and that is therefore supposed to be available for
other uses.
A pointer such as this is known as a dangling pointer and can
be the cause of troublesome problem bugs.
Avoid Dangling Pointers
delete p;
p = NULL;
Operations on Pointer Variables
Pointer Operations (1 of 4)
Assignment
Value of one pointer variable can be assigned to another pointer
of same type.
Example:
int *p, *q;
p = q;
p != q
This expression evaluates to true if p and q point to
different memory location.
Pointer Operations (3 of 4)
Some limited arithmetic operations
Here is an example of pointer declarations:
int *p;
double *q;
char *chPtr;
p++; // or p=p+1;
These statements increment the value of p by 4 bytes because p is a
pointer of type int.
q++;
chPtr++;
These statements increment the value of q by 8 bytes and the value of
chPtr by 1 byte, respectively.
Pointer Operations (4 of 4)
p = p + 2;
These statements increment the value of p by 8 bytes.