[圖解C] Example 718: dynamic allocation of a single variable

// Example 718: dynamic allocation of a single variable
// using malloc() and free() which are in <stdlib.h>

#include <stdio.h>
#include <stdlib.h>

int main()
{
    // declare a pointer, value
    // type *pointer = (type*)malloc(sizeof(type)*n), n=1 can be omit
    float *ptr = (float*)malloc(sizeof(float));
 
    printf("enter the value of P: ");
    scanf("%f", ptr);    // pointer is "address" so that no prefix "&"
    printf("\n");
 
    printf("value of P = %f\n", *ptr);
    printf("-------------------------\n");
 
    printf("release memory of P\n");
    free(ptr);
 
    printf("value of P = %f\n", *ptr);
 
    system("pause");
    return 0;
}