CSC 2105 Data Structures
Lecture 3: Pointer
Pointers
– Powerful feature of the C++ language
– Essential for construction of interesting data
structures
2
Pointers
• A pointer is a datatype.
• Contains a memory address.
• Points to a specific data type.
int *pointer1 = &x;
addr of x
int x;
0x2000
1
0x9060
&
• The address of a memory location is called a pointer.
• Every variable is assigned a memory location whose address
of this memory location can be retrieved using address
operator & of C++.
Following figure shows their memory arrangement and addresses:
&letter is 1200 , &number is 1201, &amount is 1203
Example
#include <iostream>
value FFF0 56.47
void main( ) FFF1
{ FFF2
int data = 100;
float value = 56.47; FFF3
cout << data << &data << endl; data FFF4 100
cout << value << &value << endl; FFF5
} FFF6
Output:
100 FFF4
56.47 FFF0
OR
Like any variable or constant, you must declare a pointer before you can use it
to store any variable address.
Declaration of Pointer Variables
Type *pointerVarName;
– The * before the pointerVarName indicates that this
is a pointer variable, not a regular variable
– The * is not a part of the pointer variable name
Some valid pointer declaration:
int *inp; // pointer to an integer
char *chp // pointer to a character
double *dp; // pointer to a double
float *flp; // pointer to a float
The pointer data type
A data type for containing an address (hexadecimal
number ) rather than a data value
Provides indirect access to values
The actual data type of the value of all pointers,
(integer, float, character) is the same, a long
hexadecimal number that represents a memory
address.
The only difference between pointers of different data
types is the data type of the variable that the pointer
points to.
Declaration of Pointer Variables (Cont ..)
• Whitespace doesn‘t matter and
• each of the following will declare
– ptr as a pointer (to a float) variable and
– data as a float variable
float *ptr, data;
float* ptr, data;
float (*ptr), data;
float data, *ptr;
Assignment of Pointer Variables
• A pointer variable has to be assigned a valid
memory address before it can be used in the
program
• Example:
float data = 50.8;
float *ptr;
ptr = &data;
– This will assign the address of the memory
location allocated for the floating point
variable data to the pointer variable ptr.
– This is valid, since the variable data has
already been allocated some memory space
having a valid address
Assignment of Pointer Variables (Cont
..)
FFF0
FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
data FFF4 50.8
FFF5
FFF6
11
Assignment of Pointer Variables (Cont
..)
ptr FFF0
FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
data FFF4 50.8
FFF5
FFF6
12
Assignment of Pointer Variables (Cont
..)
ptr FFF0 FFF4
FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
data FFF4 50.8
FFF5
FFF6
13
Assignment of Pointer Variables (Cont
..)
• Don’t try to assign a specific integer value to a
pointer variable since it can be disastrous
float *ptr;
ptr = 120.5; //This is wrong.
h they are both integrals
float data = 50.0;
float *ptr;
ptr = &data; //This is valid
Initializing pointers
• A pointer can be initialized during declaration by
assigning it the address of an existing variable
float data = 50.8;
float * ptr = &data;
• If a pointer is not initialized during declaration, it
is wise to give it a NULL (0) value
float *fp = NULL;
15
The NULL pointer
• The NULL pointer is a valid address for
any data type.
– But NULL is not memory address 0.
• It is an error to dereference a pointer
whose value is NULL.
– Such an error may cause your program to
crash, or behave erratically.
– It is the programmer’s job to check for this.
Dereferencing
• Dereferencing – Using a pointer variable
to access the value stored at the location
pointed by the variable
– Provide indirect access to values and also called
indirection
• Done by using the dereferencing operator
* in front of a pointer variable
– Unary operator
– Highest precedence
Dereferencing (Cont ..)
• Example:
float data = 50.8;
float *ptr;
ptr = &data;
cout << *ptr;
• Once the pointer variable ptr has been declared,
*ptr represents the value pointed to by ptr.
OUTPUT:
50.8
Dereferencing (Cont ..)
• The dereferencing operator * can also be used
in assignments.
*ptr = 200;
– Make sure that ptr has been properly initialized
float data;
float *ptr;
ptr = &data;
*ptr = 200;
cout << *ptr;
OUTPUT:
200
Dereferencing Example
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8;
float *ptr; FFF2
ptr = &data; FFF3
cout << ptr << *ptr << endl;
*ptr = 27.4; data FFF4 50.8
cout << *ptr << endl;
FFF5
cout << data << endl;
} FFF6
Output:
#include <iostream.h>
void main()
{
float data = 50.8; ptr FFF0 FFF4
float *ptr;
ptr = &data; FFF1
cout << ptr <<“ ”<< *ptr << endl;
*ptr = 27.4; FFF2
cout << *ptr << endl; FFF3
cout << data << endl;
} data FFF4 50.8
FFF5
OUTPUT: FFF6
FFF4 50.8
Dereferencing Example (Cont ..)
#include <iostream.h>
void main()
{
float data = 50.8; ptr FFF0 FFF4
float *ptr;
ptr = &data; FFF1
cout << ptr <<“ ”<< *ptr << endl;
*ptr = 27.4; FFF2
cout << *ptr << endl; FFF3
cout << data << endl;
} data FFF4 27.4
FFF5
OUTPUT: FFF6
FFF4 50.8
Dereferencing Example (Cont ..)
#include <iostream.h>
void main()
{ ptr FFF0 FFF4
float data = 50.8;
float *ptr; FFF1
ptr = &data;
FFF2
cout << ptr << *ptr << endl;
*ptr = 27.4; FFF3
cout << *ptr << endl;
cout << data << endl; data FFF4 27.4
} FFF5
OUTPUT: FFF6
27.4
Dereferencing Example (Cont ..)
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8;
float *ptr; FFF2
ptr = &data; FFF3
cout << ptr << *ptr << endl;
*ptr = 27.4; data FFF4 27.4
cout << *ptr << endl;
FFF5
cout << data << endl;
} FFF6
OUTPUT
???