Programs 9-5 to 9-8
Created for CCS120S
Array names can be used as constant pointers!
An array name, without brackets and subscript, represents the starting address of the array.
This means: arrayName == &arrayName[0]
* in C++distance = speed * time;int *ptr = nullptr;*ptr = 100;
#include <iostream>
using namespace std;
int main()
{
return 0;
}
#include <iostream>
using namespace std;
int main()
{
short numbers[] = {10, 20, 30, 40, 50};
return 0;
}
#include <iostream>
using namespace std;
int main()
{
short numbers[] = {10, 20, 30, 40, 50};
cout << "The first element of the array is ";
cout << *numbers << endl;
return 0;
}
When you add a value to a pointer, C++ adds:
value × sizeof(dataType)
For short (2 bytes):
*(numbers + 1) → address + 2 bytes*(numbers + 2) → address + 4 bytesarray[index] ≡ *(array + index)
The compiler converts subscripts to pointer arithmetic internally!
Array elements are stored in contiguous memory locations:
| 10 | 20 | 30 | 40 | 50 |
| arr[0] | arr[1] | arr[2] | arr[3] | arr[4] |
| 0x1000 | 0x1002 | 0x1004 | 0x1006 | 0x1008 |
*(arr + 2) = go to 0x1000 + (2 × 2 bytes) =
0x1004 → value: 30
| Subscript | Pointer Notation |
|---|---|
| arr[0] | *arr |
| arr[1] | *(arr + 1) |
| arr[2] | *(arr + 2) |
| arr[3] | *(arr + 3) |
| arr[4] | *(arr + 4) |
Both notations access the same memory location!
NOTE: The * operator has higher
precedence than +
| Expression | Meaning | Result |
|---|---|---|
*arr + 1 |
Get value at arr[0], then add 1 | 11 (if arr[0]=10) |
*(arr + 1) |
Move to arr[1], then get value | 20 (value at arr[1]) |
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 5;
int numbers[SIZE];
int count;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 5;
int numbers[SIZE];
int count;
// Use pointer notation instead of subscripts
cout << "Enter " << SIZE << " numbers: ";
for (count = 0; count < SIZE; count++)
cin >> *(numbers + count);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 5;
int numbers[SIZE];
int count;
// Use pointer notation instead of subscripts
cout << "Enter " << SIZE << " numbers: ";
for (count = 0; count < SIZE; count++)
cin >> *(numbers + count);
// Display using pointer notation
cout << "Here are the numbers you entered:\n";
for (count = 0; count < SIZE; count++)
cout << *(numbers + count) << " ";
cout << endl;
return 0;
}
WARNING! C++ performs NO bounds checking with arrays!
When stepping through an array with a pointer, you could accidentally access memory outside the array.
Q 9.7: Rewrite this loop using pointer notation:
for (int x = 0; x < 100; x++)
cout << arr[x] << endl;
for (int x = 0; x < 100; x++)
cout << *(arr + x) << endl;
This program demonstrates:
ptr[i]
*(arr + i)
Both work because arrays and pointers share the same underlying mechanism!
#include <iostream>
using namespace std;
int main()
{
const int NUM_COINS = 5;
double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
double *doublePtr;
int count;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int NUM_COINS = 5;
double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
double *doublePtr;
int count;
// Assign array address to pointer
doublePtr = coins;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int NUM_COINS = 5;
double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
double *doublePtr;
int count;
doublePtr = coins;
// Use subscripts with the pointer!
cout << "Here are the values in the coins array:\n";
for (count = 0; count < NUM_COINS; count++)
cout << doublePtr[count] << " ";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int NUM_COINS = 5;
double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
double *doublePtr;
int count;
doublePtr = coins;
cout << "Here are the values in the coins array:\n";
for (count = 0; count < NUM_COINS; count++)
cout << doublePtr[count] << " ";
// Use pointer notation with the array name!
cout << "\nAnd here they are again:\n";
for (count = 0; count < NUM_COINS; count++)
cout << *(coins + count) << " ";
cout << endl;
return 0;
}
You can get the address of any array element using &:
&arrayName[index]
#include <iostream>
using namespace std;
int main()
{
const int NUM_COINS = 5;
double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};
double *doublePtr = nullptr;
int count;
cout << "Here are the values in the coins array:\n";
for (count = 0; count < NUM_COINS; count++)
{
// Get the address of an array element
doublePtr = &coins[count];
// Display the contents
cout << *doublePtr << " ";
}
cout << endl;
return 0;
}
Legal:
dptr = readings; // Pointer can be reassigned
dptr = totals;
Illegal:
readings = totals; // ERROR! Cannot change array name
totals = dptr; // ERROR!
array[i] ≡ *(array + i)ptr[i]*(arr + i)