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() ?

In ANSI C, no. In K&R C yes.

> 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
> is how I have done it for years and years. Do I really need the cast?

ANSI C defines malloc() as returning a `void *', while K&R C defines
it as returning a `char *'. You can implicitly cast to or from void
pointers, but casting between other pointer types requires a cast.

> More precisely, do I really need the cast using gcc?

Not if you're using an ANSI libc (e.g. any of the ones which are used
on Linux).

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to