Documente Academic
Documente Profesional
Documente Cultură
/* display string */
printf(“String is %s\n”, str);
/* free memory */
free(str);
str=NULL;
return 0;
}
Dynamic Memory Allocation
#include <stdio.h> if(np == NULL)
#include <stdlib.h> {
int main() printf(“out of memory\n”);
{ int N,*a,*np,i,s=0; exit(1);
char ans=‘Y’; }
printf(“\n Enter no. of elements of the a = np;
array:”); }
scanf(“%d”,&N); printf(“\n Enter the number ...”);
a=(int *)malloc(N*sizeof(int)); scanf(“%d”,&a[i]);
if(a==NULL) s+=a[i];
{ printf(“\n memory allocation i++;
unsuccessful”); printf(“\n Do U 12
exit(0); Continue(y/n)?...”);
} fflush(stdin);
i=0; scanf(“%c”, &ans)
while(toupper(ans)==‘Y’) }
{ if(i >= N) N=i;
{ /* increase allocation */ printf(“\n THE NUMBERS ARE:...\n”);
N *=2; for(i=0;i<N;++i)
np =(int *)realloc(a,N*sizeof(int)); printf(“\n%d”,a[i]);
printf(“\n Sum is %d”,s);
return 0;
}