Friday, 9 November 2012


//C code for Example for Dynamic memory allocation
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
main()
{
int n,i,*ptr;
clrscr();
printf("Enter how many numbers:\n");
scanf("%d",&n);
ptr=(int *)malloc(n*sizeof(int));
if(!ptr)
{
printf("No memory for the array");
exit(1);
}
printf("Enter the element:\n");
for(i=0;i<n;i++)
{
scanf("%d",(ptr+i));
}
printf("Print array element\n");
for(i=0;i<n;i++)
{
printf("%d\n",*(ptr+i));
}
free(ptr);
getch();
}

No comments:

Post a Comment