-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDynamicMemoryAllocation1.c
44 lines (39 loc) · 1.12 KB
/
DynamicMemoryAllocation1.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
/*
@Author: Raghav Maheshwari
Memory allocation in C programs
Problem statement: In this program i am going to read and display an array using
Dynamic memory allocation functions.
*/
#include<stdio.h>
#include<stdlib.h>
void main(){
int *arr;
int n;
printf("Enter the value of n");
scanf("%d",&n);
arr = (int *)malloc(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 represents how malloc function is used in the program for allocating the memory in the heap
The syntax for doing so is very very simple
int *arr = malloc(n*sizeof(int));
arr = (int *)malloc(n*sizeof(int));
Both the statements are equally valid...
This will print the initial values as junk!!
*/
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 ",arr[i]);
}
}