-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_2D_Arrays.cpp
More file actions
47 lines (41 loc) · 1.16 KB
/
14_2D_Arrays.cpp
File metadata and controls
47 lines (41 loc) · 1.16 KB
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
#include <iostream>
using namespace std;
int main()
{
// Method 1 for Declaring and Intializing (Whole Array is inside stack)
// int arr[3][4] = {
// {1, 2, 3, 4},
// {5, 6, 7, 8},
// {9, 10, 11, 12},
// };
// Method 2 for Declaring and Intializing
// int *p = new int[3]; // not right method
// int *p[3]; // Array of Pointer p is inside stack but actual array is inside heap
// p[0] = new int[4];
// p[1] = new int[4];
// p[2] = new int[4];
// p[0][1] = 10;
// cout << p[0][1] << endl;
// Method 3 for Declaring and Intializing (to declare everything in heap we can use double pointers)
int **P = new int *[3]; // Array of 3 Pointers is Created inside heap
P[0] = new int[4];
P[1] = new int[4];
P[2] = new int[4];
// Lets initialze this array with values
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
P[i][j] = i + j + 1;
}
}
// Lets Access Elements of this array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
cout << P[i][j] << " ";
}
cout << endl;
}
}