Questions about Linked Lists?

Casting the return value of malloc

Casting the return value of malloc

par Ahmad AlLakeas,
Nombre de réponses : 1
Avatar GroupEn

malloc returns a void pointer which is automatically gets converted to the correct data type right?

if so then why do we cast the return of malloc?

En réponse à Ahmad AlLakeas

Re: Casting the return value of malloc

par Siba Haidar,
Malloc returns a void pointer (void *)
In order to access the pointer
Foe example to put value in the pointer or if the pointer is a complex type, to put values in its fields,
You need to cast so that the compiler knows how to deal with the memory space pointed to by the pointer