diff --git a/pointers/dynamicmemoryallocation.cpp b/pointers/dynamicmemoryallocation.cpp new file mode 100644 index 0000000..1665597 --- /dev/null +++ b/pointers/dynamicmemoryallocation.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; +int main() +{ + int arr[5] = {1, 2, 3, 4, 5}; + int *ptr; + ptr = new int[5]{1, 2, 3}; // memory allocated inside heap + cout << ptr[0] << endl; + ptr++; + cout << ptr[1]; + delete ptr; // prevents memory leak + return 0; +} \ No newline at end of file diff --git a/pointers/dynamicmemoryallocation.exe b/pointers/dynamicmemoryallocation.exe new file mode 100644 index 0000000..0dcce88 Binary files /dev/null and b/pointers/dynamicmemoryallocation.exe differ diff --git a/pointers/ptrdeclare.cpp b/pointers/ptrdeclare.cpp new file mode 100644 index 0000000..62f3aa5 --- /dev/null +++ b/pointers/ptrdeclare.cpp @@ -0,0 +1,14 @@ +// declaration and initialization of ptr +#include +using namespace std; +int main() +{ + int arr[5] = {1, 2, 3, 4, 5}; + int *ptr; // declaration + ptr = &arr[0]; // ptr storing address of arr 0 index value + cout << arr[0] << endl; + cout << &arr[0] << endl; + cout << ptr; + + return 0; +} \ No newline at end of file diff --git a/pointers/ptrdeclare.exe b/pointers/ptrdeclare.exe new file mode 100644 index 0000000..bf85ce9 Binary files /dev/null and b/pointers/ptrdeclare.exe differ