Question

Why do we cast malloc, as in the following?

ptd = (double *) malloc(max * sizeof(double));

What is malloc's normal return type? Why do we need to cast it?

Was it helpful?

Solution

From a standard guide:

Declaration:

void *malloc(size_t size);

Allocates the requested memory and returns a pointer to it. The requested size is size bytes. The value of the space is indeterminate. On success a pointer to the requested space is returned. On failure a null pointer is returned.

That should be everything!

OTHER TIPS

This is malloc prototype

void *malloc(size_t size);

Generally no need to do typecasting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top