-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointers2.cpp
49 lines (38 loc) · 1.26 KB
/
pointers2.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
#define LENGHT 10
class Pointers{
public:
int index = 0;
void returnArrayWhitPointer(int* ptr, int* lastPtr)
{
int* ptrc = ptr;
cout<<"while loop"<<endl;
while(ptr<lastPtr)
{
cout<<"Address: "<<ptr<<endl<<"Value: "<<*ptr<<endl<<"Index: "<<index<<endl<<"---------------"<<endl; ++ptr; ++index;
}
index = 0;
cout<<"for loop"<<endl;
// first way with for loop is copy ptr to ptrc.
for( ;ptrc<lastPtr;++ptrc)
{
cout<<"Address: "<<ptrc<<endl<<"Value: "<<*ptrc<<endl<<"Index: "<<index<<endl<<"---------------"<<endl; ++index;
}
index = 0;
// second way with for loop is substract the lenght from ptr
cout<<"for loop's second way"<<endl;
for(ptr = ptr-LENGHT;ptr<lastPtr;++ptr)
{
cout<<"Address: "<<ptr<<endl<<"Value: "<<*ptr<<endl<<"Index: "<<index<<endl<<"---------------"<<endl; ++ptr; ++index;
}
}
};
int main() {
int array[10] = {1,2,3,4,5,6,7,8,9,10};
int* ptr = array;
int* lastPtr = ptr + LENGHT;
Pointers slave;
slave.returnArrayWhitPointer(ptr,lastPtr);
return 0;
}