Skip to content

Commit f0942d2

Browse files
authored
Update 01 static and dynamic array .c
1 parent 73a83eb commit f0942d2

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

Array/01 static and dynamic array .c

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
#include<stdio.h>
2-
#include<stdlib.h>
3-
int main ()
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
int main()
44
{
5-
int A[5]={2,3,4,6,7}; // this will allocate memory in stack memory
6-
int *p;
5+
int A[5] = { 1,2,3,4,5 }; // this is allocated inside stack memory
6+
int* p; // pointer of array
77
int i;
8-
9-
p= (int *)malloc(5*sizeof(int)); // using malloc we can access memory in heap
10-
p[0]=8;
11-
p[1]=7;
12-
p[2]=9;
13-
p[3]=10;
14-
p[4]=12;
15-
16-
for (i=0;i<5;i++)
8+
p = (int*)malloc(5 * sizeof(int)); // dynamically createing array inside heap
9+
p[0] = 1; // initializing all the terms in array
10+
p[1] = 2;
11+
p[2] = 3;
12+
p[3] = 4;
13+
p[4] = 5;
14+
15+
for (i = 0; i < 5; i++) // iteration for normal array
1716
{
18-
printf("%d ",A[i]);
19-
17+
printf("%d ", A[i]);
2018
}
2119
printf("\n");
22-
for (i=0;i<5;i++)
20+
for (i = 0; i < 5; i++) // iteration for the dynamic array
2321
{
2422
printf("%d ",p[i]);
25-
2623
}
2724
return 0;
2825
}

0 commit comments

Comments
 (0)