-
Notifications
You must be signed in to change notification settings - Fork 0
/
arraysAndPointers.cpp
83 lines (68 loc) · 1.7 KB
/
arraysAndPointers.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
using namespace std;
void incrementBy10(int *arrPointer, int size);
void incrementBy100(int *mArr, int size);
int *multiplyBy100(int *mArr, int size);
void preview(int mArr[], int size);
void preview2(int *mArr, int size);
int main()
{
// In CPP, the name of the array is basically a
// pointer that stores the memory location of the
// first element of the array;
// Therefore,
int mArr[] = {10, 20, 30, 40, 50};
// here, 'mArr' is a pointer to 10 (mArr[0])
int *pointer;
pointer = mArr;
// cout << "Value of 0th element: " << *pointer << endl;
// cout << "Value of 1st element: " << *(pointer + 1) << endl;
int length = sizeof(mArr) / sizeof(int);
incrementBy100(mArr, length);
int *nArr;
nArr = multiplyBy100(mArr, length);
preview2(nArr, length);
return 1;
}
void incrementBy10(int mArr[], int size)
{
for (int i = 0; i < size; i++)
{
mArr[i] += 10;
}
}
void incrementBy100(int *mArr, int size)
{
for (int i = 0; i < size; i++)
{
*(mArr + i) += 100;
}
}
// If we want out function to return an arr.
// Then the function should return a pointer of that arr.
// Also, C++ will not allow the address of the variable
// to be shared among other functions, therefore, we need to
// use 'static'.
int *multiplyBy100(int *mArr, int size)
{
static int nArr[5];
for (int i = 0; i < size; i++)
{
nArr[i] = *(mArr + i) * 100;
}
return nArr;
}
void preview(int mArr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << mArr[i] << endl;
}
}
void preview2(int *mArr, int size)
{
for (int i = 0; i < size; i++)
{
cout << *(mArr + i) << endl;
}
}