I was learning about malloc
, calloc
, free
and realloc
in C but I don't get when should I use these functions.
For example: if I want to create a program in C that creates an array and the size of it is determined from user input:
int n;
printf("Enter THe Number of element in Array:..\n");
scanf("%d",&n);
int x[n];
for(int i = 0 ; i < n ; i++)
{
x[i] = i+ 1;
}
for(int y = 0 ; y < n ; y++)
{
printf("%d\n",x[y]);
}
output:
Enter THe Number of element in Array:..
5
1
2
3
4
5
So here I created this program without using Dynamic memory allocation, which makes things more complicated to me.