-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.4malloc.c
49 lines (40 loc) · 886 Bytes
/
1.4malloc.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
46
47
48
49
//yeh diye gye hisab se ek block create krta hai
//ex:-ptr=(int*)malloc(10);
//yeh 10 byte ka block bnayega =based on bit architecture
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n,*ptr,sum=0,i;
int *p;
printf("Enter The size of array");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
p=ptr; //get first block address of dynamic array
//Check array created or not
if(ptr==NULL)
{
printf("Out Of memory");
exit(0);
}
//Enter values from user
printf("enter the elements of array");
for(i=1;i<=n;i++)
{
scanf("%d",ptr); //store value to first block address
sum=sum+*ptr; //sum of each value
ptr++; //increment value for next block
}
//print values
printf("elements are:");
for(i=1;i<=n;i++){
printf("%d\n", *p);
p++;
}
//finally print addition of sum
printf("addition is %d",sum);
return 0;
//by Navjot Singh Prince:)
}