Dynamic allocation of memory using malloc() function
#include<conio.h> #include<stdio.h> #include<stdlib.h> void main() { int *ptr,n,i; printf("Enter the number of integer to be entered :-"); scanf("%d",&n); ptr=(int*)malloc(n*sizeof(int)); if(ptr==NULL) { printf("Memory not available."); exit(0); } printf("Enter %d numbers:-",n); for(i=0;i<n;i++) scanf("%d",ptr+i); printf("Entered numbers is :-"); for(i=0;i<n;i++) printf("%3d",*(ptr+i)); free(ptr); getch(); }Output:-
0 Comments