-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDynamicMemoryAllocation2.c
39 lines (35 loc) · 1012 Bytes
/
DynamicMemoryAllocation2.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
/*
@Author: Raghav Maheshwari
This code is supposed to read an integer array and the print it.
*/
#include<stdio.h> //For all the functions we use like printf and scanf
#include<stdlib.h> //For usimg the dynamic memory allocation functions.
void main(){
printf("Enter the size of the array");
int n;
scanf("%d",&n);
int *arr = calloc(n,sizeof(int));
/*
The following block of code is executd if memory could not be allocated to tthe array
*/
if(arr == NULL){
printf("MEMORY CANNOT BE ALLOCATED");
exit(0);
}
/*
This was the dynamic memory declaration using the calloc function
int *arr;
arr = calloc(n,sizeof(int));
This differs from malloc in the way that it initializes all the array values to zero.
*/
for(int i=0;i<n;i++){
printf("%d",arr[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(int i=0;i<n;i++){
printf("%d\n",arr[i]);
}
free(arr);
}