Brief Introduction to Pointers
Brief Introduction to Pointers
POINTERS IN C++
• int *ptr;
ptr = &count // Stores the address of count in ptr
// The unary operator & returns the address of a variable
• To get the value that is stored at the memory location in the pointer
it is necessary to dereference the pointer. Dereferencing is done
with the unary operator "*".
• int total;
total = *ptr;
// The value in the address stored in ptr is assigned to total
• The best way to learn how to use pointers is by example. There are
examples of the types of operations already discussed below.
Pointers are a difficult topic. Don't worry if everything isn't clear yet.
Declaration and Initialization
• Declaring and initializing pointers is fairly easy.
• int main()
{
int j;
int k;
int l;
int *pt1; // Declares an integer pointer
int *pt2; // Declares an integer pointer/
float values[100];
float results[100];
float *pt3; // Declares a float pointer
float *pt4; // Declares a float pointer
j = 1;
k = 2;
pt1 = &j; // pt1 contains the address of the variable j
pt2 = &k; // pt2 contains the address of variable k
pt3 = values;
// pt3 contains the address of the first element of values
pt3 = &values[0];
// This is the equivalent of the above statement
return 0;
}
Pointer Dereferencing/Value Assignment
• Dereferencing allows manipulation of the data contained at the
memory address stored in the pointer.
• The pointer stores a memory address. Dereferencing allows the
data at that memory address to be modified. The unary operator "*"
is used to dereference.
For instance:
• *pt1 =*pt1 + 2;
• This adds two to the value "pointer to" by pt1. That is, this statement
adds 2 to the contents of the memory address contained in the
pointer pt1. So, from the main program, pt1 contains the address of
j. The variable "j" was initialized to 1. The effect of the above
statement is to add 2 to j.
• The contents of the address contained in a pointer may be assigned
to another pointer or to a variable.
• *pt2 = *pt1;
// Assigns the contents of the memory pointed to by pt1
// to the contents of the memory pointer to by pt2;
k = *pt2;
// Assigns the contents of the address pointer to by pt2 to k.
Pointer Arithmetic
• Part of the power of pointers comes from
the ability to perform arithmetic on the
pointers themselves.
• Pointers can be incremented,
decremented and manipulated using
arithmetic expressions. Recall the float
pointer "pt3" and the float array "values"
declared above in the main program
pt3 = &values[0];
// The address of the first element of "values" is stored in pt3
pt3++;
// pt3 now contains the address of the second element of values
*pt3 = 3.1415927;
// The second element of values now has pie (actually pi)
pt3 += 25;
// pt3 now points to the 27th element of values
*pt3 = 2.22222;
// The 27th element of values is now 2.22222
pt3 = values;
// pt3 points to the start of values, now
for (ii = 0; ii < 100; ii++)
{
*pt3++ = 37.0; // This sets the entire array to 37.0
}
pt3 = &values[0];
// pt3 contains the address of the first element of values
pt4 = &results[0];
// pt4 contains the address of the first element of results
for (ii=0; ii < 100; ii++)
{
*pt4 = *pt3;
// The contents of the address contained in pt3 are assigned to
// the contents of the address contained in pt4
pt4++;
pt3++;
}
Example of dynamic arrays code
• /* This program illustrates the use of dynamic arrays. It prompts the user for a list of integers, then outputs
their average to the screen.
• */
• #include <iostream.h>
• #include <stdlib.h>
• #include <stddef.h> /* Function to compute the average value of the integer elements in an array "list[]" of
length "length" */
• float average(int list[], int length);
• /* MAIN PROGRAM */
• int main()
• { int no_of_integers, *number_ptr;
• cout << "Enter number of integers in the list: ";
• cin >> no_of_integers;
• number_ptr = new int[no_of_integers];
» if (number_ptr == NULL)
» { cout << "Sorry, ran out of memory.\n";
exit(1); }
» cout << "type in " << no_of_integers; cout << " integers sepatated by spaces:\
n"; for (int count = 0 ; count < no_of_integers ; count++)
cin >> number_ptr[count]; cout
<< "Average: " << average(number_ptr,no_of_integers) << "\n"; delete
[] number_ptr; return 0;}/* END OF MAIN PROGRAM
*/ /* DEFINITION OF TWO ARGUMENT FUNCTION "average" */float average(int list[],
int length){ float total = 0; int count; for (count = 0 ;
count < length ; count++) total += float(list[count]); return