[LCP]sizeof question.
Greg Black
gjb at gbch.net
Fri Sep 20 17:42:01 UTC 2002
ianezz at sodalia.it wrote:
| 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?
|
| [...]
|
| calloc() returns a void *, so in turn one needs a cast.
|
| What is the problem?
Read the Standard. You'll see that the cast is not needed. Use
of unneeded casts is at best unwise (wrong, as far as I'm
concerned) because it can mask real errors from the compiler.
This has been covered on this list before.
The rule is simple -- none of the malloc family requires a
cast. Very few casts are required in Standard C and most uses
that people make of casts are ill-advised.
If this is still confusing, read a good book on C (but be aware
that at least 95% of all books on C are pathetic).
Greg
More information about the linuxCprogramming
mailing list