[LCP]sizeof question.

ianezz at sodalia.it ianezz at sodalia.it
Fri Sep 20 17:15:02 UTC 2002


Il clone di Greg Black ha segretamente scritto:
 > David wrote:
 > 
 > | 	struct _Options *ptr;
 > | 
 > | 	ptr = (struct _Options *) calloc (1, sizeof *ptr);
 > 
 > Lose the cast ^^^^^^^^^^^^^^^^^^^ -- it's wrong.

Uh?

ptr is a pointer to a struct _Option.

sizeof(ptr)  == sizeof(struct _Option *)  (i.e. the size of a pointer).

sizeof(*ptr) == sizeof(struct _Option)    (i.e. the size of the struct).

calloc() returns a void *, so in turn one needs a cast.

What is the problem?

-- 
 |   \    \  | ___|_  |_  | ianezz AT sodalia.it
 |  _ \  | \ | _|    /   /  Visita il LinuxTrent a
_|_/  _\_|  _|____|___|___| http://www.linuxtrent.it



More information about the linuxCprogramming mailing list