-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes_2_pointers.cpp
81 lines (61 loc) · 2.04 KB
/
notes_2_pointers.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
#include <iostream>
using namespace std;
template <typename T>
size_t getWrongArrSize(T arr[])
{
return sizeof(arr);
}
template <typename T, size_t n>
int getCorrectArrSize(T (&arr)[n])
{
return sizeof(arr);
}
template <typename T, size_t n>
void print(T (&arr)[n])
{
size_t size = getCorrectArrSize(arr);
for (size_t i = 0; i < size / sizeof(arr[0]); i++)
{
cout << "arr[" << i << "]: " << arr[i] << endl;
}
}
int main()
{
// Another interesting thing between arr and
// pointers.
// the variable used for array is just a pointer
// to the first element in that array.
// for eg.
int arr[] = {14, 34, 56, 6, 42};
// here 'arr' is just another pointer; that points to
// the first element's address, that is, 1.
// its similar to=> int *arr = &temp; { temp = 14; }
// therefore,
cout << (arr[0] == *arr) << endl; // true: 14
cout << (arr[1] == *(arr + 1)) << endl; // true: 34
// we can do *(arr + 1), because the memory is
// consecutive in case of an array.
cout << "Memory address of 14: " << arr << endl;
cout << "Memory address of 34: " << (arr + 1) << endl;
// Furthermore, there's one interesting thing I notice.
cout << "sizeof(arr): " << sizeof(arr) << endl; // 20 (4[int] * 5[len])
cout << "getWrongArrSize(arr): " << getWrongArrSize(arr) << endl; // 8 {WRONGGGGG!!! SO WRONG! OMG!}
// But why?
// the above function simply returns the size of the pointer.
// as described above 'arr' is just a pointer. Therefore,
// the 8 it returned is just a pointer size.
// Solution?
// Sending as arr-reference;
// SYNTAX: (&arr)[];
// Yeah I know!
// Now
cout << "arr : " << arr << endl;
cout << "getCorrectArrSize(arr): " << getCorrectArrSize(arr) << endl; // 20 Correct!
cout << "\n\nINT ARR: " << endl;
int i_arr[] = {52, 6, 45, 64, 6756};
print(i_arr);
cout << "\nSRING ARR: " << endl;
string s_arr[] = {"asd", "saf", "345", "name"};
print(s_arr);
return 1;
}