On Thu, Apr 01, 1999 at 07:34:57PM +0000, holotko wrote:
> Let's say I want to dynamically allocate some memory to an object that
> I am creating using malloc(). Traditionally I do the following:
>
> ObjectType *objectpointer; /* Decl. of pointer to object */
>
> objectpointer = (ObjectType *)malloc(sizeof(ObjectType));
>
> My question. Do I really need to add the type cast (ObjectType *)
> before the call to malloc() ?
No. malloc() returns a void * which is automatically converted to the
correct type upon assignment. In general, you can assign to and from
void pointers without loss of information.
> Traditionally I have always been taught to include the cast. However,
> some references say that a cast was ONLY required in earlier versions
> of C prior to standard ANSI C. Nonetheless I always type cast as that
The cast was required in pre-ANSI days.
Kurt