-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointerArray.cpp
More file actions
32 lines (32 loc) · 1 KB
/
Copy pathpointerArray.cpp
File metadata and controls
32 lines (32 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
int main() {
int numbersArray[5];
int *pPointer = nullptr;
pPointer=numbersArray;
//assign the address to the first element to the pointer
pPointer = numbersArray;
*pPointer = 10; //assign a value to the first element
/*increment the pointer using pointer arithmetic
to assign the address of the second element to the pointer*/
pPointer++;
*pPointer = 20; //assign a value to the second element
//assign the address of the third element to the pointer
pPointer = &numbersArray[2];
*pPointer = 30; //assign a value to the third element
/*assign the address of the fourth element to the pointer using pointer arithmetic*/
pPointer = numbersArray + 3;
*pPointer
=
40;
//assign a value to the fourth element
//assign the address to the first element to the pointer pPointer = numbersArray;
pPointer= &numbersArray[4];
*pPointer=50;
//iterate and output all the elements in the array
for (int n = 0; n < 5; n++)
{
cout << numbersArray[n] << ", ";
}
return 0;
}