Skip to content

Commit

Permalink
Structures in cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishak798 authored Feb 11, 2024
1 parent 568a0d4 commit abcb095
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Structures/Heapmemo.cpp
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;
}
21 changes: 21 additions & 0 deletions Structures/heapmember.cpp
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 added Structures/heapmember.exe
Binary file not shown.
13 changes: 13 additions & 0 deletions Structures/pointer.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 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 added Structures/pointer.exe
Binary file not shown.
20 changes: 20 additions & 0 deletions Structures/pointertostruc.cpp
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 added Structures/pointertostruc.exe
Binary file not shown.
9 changes: 9 additions & 0 deletions Structures/refrence.cpp
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 added Structures/refrence.exe
Binary file not shown.
16 changes: 16 additions & 0 deletions Structures/structure.cpp
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 added Structures/structure.exe
Binary file not shown.

0 comments on commit abcb095

Please sign in to comment.