Dynamic Memory Allocation in C
Get Certified in C Programming and Take Your Skills to the Next Level
Program 1
// Program for Dynamic memory
#include<stdio.h>
#include<conio.h>
int main()
{
int *ar,n,i,s,f=0;
system("cls");
printf("Enter size of an array");
scanf("%d",&n);
ar=(int*)calloc(sizeof(int),n); // allocated dynamic memory
printf("\nEnter elements in array");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
printf("\n Elements are: ");
for(i=0;i<n;i++)
printf("%d ",ar[i]);
printf("\nEnter an element for search");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(ar[i]==s)
{
f=1;
break;
}
}
if(f==1)
printf("\n searching success");
else
printf("\n searching not success");
free(ar); // deallocated dynamic memory
return 0;
}
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

