pointer
• What it is
• How to declare it
• How to use it
• Relationship between arrays and pointers
• Relationship between strings and pointers
How to initialize a pointer to point to an address
count
countPtr
memory address 100010
1000
int count = 10; int * countPtr = &count;
& is an operator that returns the address of a variable.
Restrictions for using & operator• May be used for variables
int count;
int *countPtr = &count;
• May not be used with constants (&10)
Indirect and direct references
Direct reference – changing count memory location using count variable
Indirect reference – changing count memory location using countPtr variable
count
10
countPtr
Indirect and direct references
int count; int * countPtr = &count;
count = 12; // direct reference*countPtr = 12 // indirect reference
* is the indirection operator. Means change the memory location currently pointed to by countPtr.
count
10
countPtr
12
Exercises
1) Declare an integer variable called x. Declare a pointer to x called xPtr and initialize it so that it points to the variable x. Use xPtr to indirectly change the value of x to 20;
2) Declare a second integer variable called y. Declare a pointer to y called yPtr and initialize it so that it points to the variable y. Calculate the sum of x + y using xPtr and yPtr.
pointer arithmetic
int *aPtr;aPtr++; // is valid but what does it do?
integer (4 bytes) aPtr
aPtr is incremented by the size of whatever it is pointing to. In this example 4 bytes. So the value in aPtr is increased by 4 by the aPtr ++ statement
pointer arithmetic
char *aPtr;aPtr++;
char (1 byte) aPtr
aPtr is incremented by the size of whatever it is pointing to. In this example 1 byte. So the value in aPtr is increased by 1 by the aPtr ++ statement
using pointer arithmetic
grades[0]grades[1]grades[2]grades[3]grades[4]
10085506588
int gradesPtr = &grades[0];
gradePtr
gradesPtr++; gradePtr
using pointer arithmetic
letters[0]letters[1]letters[2]letters[3]letters[4]
‘a’‘b’‘c’‘d’‘e’
char letterPtr = &letters[0];
letterPtr
letterPtr ++; letterPtr
pointers and arrays
• array name is a pointer. This is why:can pass array name to a function without the
bracketsarrays are passed to functions as reference
parameters by default
arrays as function parametersPass the name of the array without the brackets
void PrintResults(int results[ ]); // function prototype
void PrintResults(int results[ ]) // function definition{
}
calling sequence: const int maxRange = 10; int frequency[maxRange ] = {0}; PrintResults (frequency);
pointers and arrays
int *bPtr; int b[ ] = {1,2,3,4,5};
bPtr = &b[0]; or bPtr = b;
b[0]b[1]b[2]b[3]b[4]
12345
bPtr
b
pointers and strings
char digits [ ] = {“0123456789”};
‘0’‘1’
‘3’‘4’‘5’‘6’‘7’‘8’‘9’
digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
‘2’
NULL
pointers and strings
char digits [ ] = {“0123456789”};
‘0’‘1’
‘3’‘4’‘5’‘6’‘7’‘8’‘9’
digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
‘2’
NULL
char *digitsPtr = digits;
digitsPtr
simplify
Instead of char digits [ ] = {“0123456789”};
char *digitsPtr = digits;
do char *digitsPtr = “0123456789”;
simplify
char *digitsPtr = “0123456789”;
cout << digitsPtr << endl; // displays all the numbers in the string
Simplify Array of strings
const int Size = 5; const int WordSize = 10;
char article [Size] [WordSize] = {"the", "a", "one", "some", "any"}; char noun [Size] [WordSize] = {"boy", "girl", "dog", "town", "car"}; char verb [Size] [WordSize] = {"drove", "jumped", "ran", "walked", "skipped"}; char preposition [Size] [WordSize] = {"to", "from", "over", "under", "on"};
Array of strings
const int Size = 5; char *article [Size] = {"the", "a", "one", "some", "any"}; char *noun [Size] = {"boy", "girl", "dog", "town", "car"}; char *verb [Size] = {"drove", "jumped", "ran", "walked", "skipped"}; char *preposition [Size] = {"to", "from", "over", "under", "on"};
Top Related