explain malloc() function and its type casting
how execute

What is there to explain? malloc (attempts to) allocate a given amount of memory on the heap. It doesn't really know (or cares) what you're going to put there so it returns a void pointer so you'll have to cast it to a pointer matching the data you're putting in it. (atleast if you want to dereference it for example)

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

int main(void)
{
    int* heap_int = NULL;

    // Allocate memory for a single integer.
    heap_int = (int*) malloc(sizeof(int));

    if (heap_int == NULL)
        printf("Memory allocation failed.\n");
    else
    {
        printf("Memory was successfully allocated.\n");

        // Free the memory again.
        free(heap_int);
    }
    return 0;
}

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.