-
Notifications
You must be signed in to change notification settings - Fork 2
/
2d_array_declaration.c
83 lines (69 loc) · 1.83 KB
/
2d_array_declaration.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/****************************************************************************
File name: 2d_array_declaration.c
Author: babajr
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
/* Method 1: 2D array on stack */
int a[3][4] = {{1, 2, 3, 4},
{4, 5, 6, 7},
{7, 8, 8, 9}};
int i, j;
printf("Display the array created on stack\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
/* Method 2: 2D array with single pointer to array.
Pointers will be created in stack and rows will be created in heap.*/
int *b[3];
b[0] = (int *)malloc(4 * sizeof(int));
b[1] = (int *)malloc(4 * sizeof(int));
b[2] = (int *)malloc(4 * sizeof(int));
printf("Enter Elements\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
scanf("%d ", &b[i][j]);
}
}
printf("Display the array created on partially on heap\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
printf("%d ", b[i][j]);
}
printf("\n");
}
/* Method 3: Rows and columns are created in heap */
int **c;
c = (int **)malloc(3 * sizeof(int *));
c[0] = (int *)malloc(4 * sizeof(int));
c[1] = (int *)malloc(4 * sizeof(int));
c[2] = (int *)malloc(4 * sizeof(int));
printf("Enter Elements\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
scanf("%d ", &c[i][j]);
}
}
printf("Display the array created on heap\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
printf("%d ", c[i][j]);
}
}
return 0;
}