-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp-data-structures-pointers.cpp
57 lines (36 loc) · 1.02 KB
/
cpp-data-structures-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
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int a = 10;
int *p; // address variable declaration
p = &a; // pointer variable initialization
cout << a <<endl;
cout << "Using pointer " << *p<<endl; //dereferencing
int A[5] = { 2,4,6,8,10 };
int *z;
z = A; // p = &A[0] is same as p = A;
// the address is already the first element in the array, so "&" unnecessary
for (int i = 0; i < 5; i++) {
cout << z[i] << endl; // pointer works to access array without *
};
int* b;
b = new int[5];
b[0] = 11; b[1] = 12; b[2] = 13; b[3] = 14; b[4] = 15;
for (int i = 0; i < 5; i++) {
cout << b[i] << endl; // pointer works to access array without *
};
delete[] b; // deallocating memory from Heap
int* p1;
char* p2;
float* p3;
double* p4;
struct Rectangle* p5;
cout << sizeof(p1) << endl; // 8 bytes
cout << sizeof(p2) << endl; // 8 bytes
cout << sizeof(p3) << endl; // 8 bytes
cout << sizeof(p4) << endl; // 8 bytes
cout << sizeof(p5) << endl; // 8 bytes
return 0;
}