-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
568a0d4
commit abcb095
Showing
11 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
int main() | ||
{ | ||
int *p; | ||
// allocate memory in heap--> create an array of sixe 5 in heap | ||
p = (int *)malloc(5 * sizeof(int)); // int arr[5];//c-lang | ||
p = new int[5]; // cpp-lang | ||
// Allocate memory in heap - create an array of size 5 in heap | ||
p = new int[5]; // C++ way of allocating dynamic memory | ||
|
||
// Inserting values into the dynamically allocated array | ||
for (int i = 0; i < 5; i++) | ||
{ | ||
p[i] = i + 1; // Assigning values to array elements | ||
} | ||
|
||
// Printing the values stored in the dynamically allocated array | ||
for (int i = 0; i < 5; i++) | ||
{ | ||
cout << p[i] << " "; | ||
} | ||
cout << endl; | ||
|
||
// Don't forget to deallocate memory to avoid memory leaks | ||
delete[] p; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
struct Rectangle | ||
{ | ||
/* data */ | ||
int length; | ||
int breadth; | ||
}; | ||
|
||
int main() | ||
{ | ||
// pointer to structure | ||
struct Rectangle *p; | ||
p = new Rectangle; | ||
p->length = 10; | ||
p->breadth = 20; | ||
cout << "value of length: " << p->length << endl; | ||
cout << "value of breadth: " << p->breadth << endl; | ||
cout << "Area: " << (p->length) * (p->breadth) << endl; | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
int main() | ||
{ | ||
int a = 10; | ||
// declare a pointer to store address of a | ||
int *p; // declaration | ||
p = &a; // initialization | ||
cout << "Adress of var a: " << p << endl; | ||
cout << "Value of var a accessed by pointer(derefrencing): " << *p << endl; | ||
|
||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
struct Rectangle | ||
{ | ||
/* data */ | ||
int length; | ||
int breadth; | ||
}; | ||
|
||
int main() | ||
{ | ||
struct Rectangle r = {10, 30}; | ||
// pointer to structure | ||
struct Rectangle *p = &r; | ||
cout << "value of length: " << (*p).length << endl; | ||
cout << "value of breadth: " << (*p).breadth << endl; | ||
cout << "Area: " << ((*p).length) * ((*p).breadth) << endl; | ||
|
||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
int main() | ||
{ | ||
int a = 10; | ||
int &r = a; // data having two names orignal and a nickname | ||
cout << "value of r: " << r << endl; | ||
cout << "value of a: " << a; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
// structure define | ||
struct Rectangle | ||
{ | ||
int length; | ||
int breadth; | ||
}; | ||
int main() | ||
{ | ||
struct Rectangle r; | ||
r.length = 10; | ||
r.breadth = 20; | ||
cout << "Area of Rectangle: " << (r.length) * (r.breadth); | ||
return 0; | ||
} |
Binary file not shown.