Skip to content

Commit

Permalink
Merge pull request #32 from Manishak798/Manishak798-patch-30
Browse files Browse the repository at this point in the history
C++ Pointers
  • Loading branch information
Manishak798 authored Jan 20, 2024
2 parents 4c0d770 + 3d88b87 commit 8a4e4c4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pointers/dynamicmemoryallocation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <bits/stdc++.h>
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;
}
Binary file added pointers/dynamicmemoryallocation.exe
Binary file not shown.
14 changes: 14 additions & 0 deletions pointers/ptrdeclare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// declaration and initialization of ptr
#include <bits/stdc++.h>
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;
}
Binary file added pointers/ptrdeclare.exe
Binary file not shown.

0 comments on commit 8a4e4c4

Please sign in to comment.