Allocate Memory for an Integer Variable Dynamically in C programming.
In this code snippet we will learn how to declare memory at run time for an integer variable, Dynamic Memory Allocation can be done by malloc() function in C programming language.
C Code Snippet - Dynamic Memory Allocation for an Integer Variable
/*Allocate memory for an integer dynamically.*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr=(int*)malloc(1*sizeof(int));
*ptr=10;
printf("value is: %d, address if: %u\n",*ptr, ptr);
free(ptr);
return 0;
}
value is: 10, address if: 15020048