-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path06-malloc.c
45 lines (33 loc) · 855 Bytes
/
06-malloc.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
// Program to calculate the sum of n numbers
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, sum = 0;
void *ptr;
printf("Enter number of elements: ");
scanf("%d", &n);
// 52 ------
// float -> int: 4, float: 8 float-> 52, 60, 68 ...., int -> 52, 56
// n = 5, 5*4 = 20 bytes
ptr = malloc(n * sizeof(int)); // --> Casting the TYPE here
float c = 1;
printf("\n%f", c);
// if memory cannot be allocated
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *((int*)(ptr + i));
}
printf("\nArray Contents Now\n");
for(i = 0; i < n; ++i) {
printf("%d\n", *((int*)(ptr + i)));
}
printf("\nSum = %d\n", sum);
// deallocating the memory
free(ptr);
return 0;
}