We can create both static and dynamic array in C. These arrays can be one dimensional or multiple dimensional. In statically allocated array problem is that we have to specify the size of the array before the compilation. So the problem is generated when we don’t know how much size of the array required ahead of time.
We can resolve these issues using dynamic memory allocation. The advantage of a dynamically allocated array is that it is allocated on the heap at runtime. The C language provides library function to request for the heap memory at runtime.
In the below program, I am using malloc to allocate the dynamic memory for the 1D and 2D array.
Syntax of malloc in C
Parameters
size ==> This is the size of the memory block, in bytes.
Return Value:
Returns a pointer to the allocated memory, if enough memory is not available then it returns NULL.