forked from MadhavBahl/OOPS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmalloc.c
More file actions
30 lines (23 loc) · 782 Bytes
/
malloc.c
File metadata and controls
30 lines (23 loc) · 782 Bytes
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
/* ============================================================================ */
/* ===== Program to find the sum of elements in array (Dynamic mem alloc) ===== */
/* ============================================================================ */
#include<stdio.h>
#include<stdlib.h>
int main() {
int num, i, *ptr, sum=0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*)malloc(num * sizeof(int));
if(ptr == NULL) {
printf("Error! memory not allocated.");
return 0;
}
printf("Enter the elements of array(separated by space): ");
for(i=0;i<num;i++) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum of elements of the array is: %d\n",sum);
free(ptr);
return 0;
}