Best C Programming code for representing Dynamic Memory Allocation using malloc()-February,2021
Introduction
This code is representing Dynamic Memory Allocation topic in detail. This topic is very much important for understanding pointer and its use.
Explanation
In this code you will be able to learn Dynamic Memory Allocation(DMA). The code is going to use array representation through DMA. As you all know array is static memory allocation and It’s totally true. But in this code I am going to show that how you can represent array as partial dynamic.
Partial dynamic means you can ask the user about the size of the array. This was not possible in normal array declaration. In normal array declaration, you specify the size of the array during declaration. But in this code I am going to ask the user to enter the size of the array and according to that I will occupy memory. I am going to use pointer to solve that issue.
In this code I am going to use malloc() for occupying the memory. This function with the help of it’s parameter can occupy some memory and return the generic type address.
I hope you are going to get the complete idea from the code. If you are facing any problem then please feel free to email me : creativeresource2020@gmail.com.
You can also put your valuable feedback and opinion in the comment section.
You can also visit my Youtube channel :
https://www.youtube.com/channel/UC51-9SUu0-CaAYwT4XLNtSw
#include<stdio.h> int main() { int *p,m,i;// printf("Enter the number of elements : "); scanf("%d",&m); p=(int *)malloc(m*sizeof(int)); printf("Enter the values : "); for(i=0;i<m;i++) { scanf("%d",(p+i));//(0x90 + 0) /// (0x90 + 1) } printf("Values are : "); for(i=0;i<m;i++) { printf("%d,",*(p+i)); } }