-->

program to understand about malloc() function

//understanding malloc()
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *x ;
    x= (int*)malloc(sizeof(int));//allocates the memory(4 bytes) to heap
    if(x==NULL)//tests whether the memory is available
    {
        printf("error in allocating");//displays error if no memory
    }
    else
    {
        *x=10;//assigns value
    printf("%d\n", *x);//prints value 10
    }
    return 0;
}

No comments:

Post a Comment