Now suppose that you want to assign a trade
pointer to the address of
a chunk of memory removed from the free store for a trade
object.
Suppose tptr
is the pointer and
malloc (sizeof (trade struct))
is the expression producing the
address of the memory chunk. You might think that the following would
assign the proper value to tptr
:
tptr = malloc (sizeof (struct trade)); /* DEFECTIVE */
The statement looks like it should work. After all, tptr
is a
variable whose value is supposed to be an address, and malloc
is a
function that is supposed to return an address. The problem is that the
address returned by malloc
is not the same as the type declared for
tptr
. Accordingly, C requires you to cast the value
returned by malloc
, thus converting that value into the type
required for assignment to the tptr
object pointer.